2015-02-10 60 views
0

大家好,我需要一些幫助編輯基於該網站一個API範圍網址這樣的範圍,如果我有foo.com/?Wab_id=15將編輯API範圍編輯API URL

?scope=accepted&view=films&wab_id=15 

我想我可以有一些像

$ApiData = file_get_contents('http://foo.com/api/?scope=accepted&view=films/&wab_id=$id'); 

,然後利用獲取檢索ID這就是被傳遞到URL編輯API的URL。我也嘗試循環雖然整個JSON,然後調用數組內的一個關鍵,但也沒有獲得太多的運氣我的代碼如下

$ApiData = file_get_contents('http://foo.com/api/?scope=accepted&view=films'); 
$obj = json_decode($ApiData, true); 
$data = $obj; 
//here you load $data with whatever you want. 
$id = $_GET['id']; 
foreach ($data[$id] as $key=>$value){ 
echo "$key -> $value<br>"; 
} 
?> 

但這返回一個錯誤

Invalid argument supplied for foreach() 

IVE還試圖通過穆蒂陣列一個foreach循環一個foreach的內側和已經顯示的值的代碼和結果如下

$obj = json_decode($ApiData, true); 
$data = $obj; 
//here you load $data with whatever you want. 

foreach ($data as $film){ 
    foreach ($film as $key=>$val){ 
    echo "$key -> $val<br>"; 
    } 
} 

結果

uid -> 95 
wab_id -> 95 
title -> La Batalla de los Invisibles 
title_en -> Battle of the Invisibles 
syn_sm -> 
syn_med -> 
syn_lg -> 
form -> 
genre -> 
language -> 
subtitle_lang -> 
year -> 
runtime -> 
place_sub_city -> 
place_sub_state -> 
place_sub_country -> 
place_film_country -> Mexico 
place_dir_city -> 
place_dir_state -> 
place_dir_country -> 
accepted -> 1 
festival_year -> 2014 
trailer -> 
links -> 
+0

我不知道你問什麼,但可以肯定的循環int是不是一個好主意。此外,最好使用CURL而不是file_get_contents()來檢索網站內容 – Robert 2015-02-10 23:53:09

+0

這可能是因爲你沒有設置$ data [95]。 – Darren 2015-02-11 00:03:54

+0

您是否將'wab_id','Wab_id'或'id'傳遞到您要使用'$ _GET'訪問的服務器?因爲它不清楚你將自己傳遞給自己的關鍵在哪裏,如果你使用的是正確的關鍵 - 這可能是你現在遇到的問題。 – prodigitalson 2015-02-11 00:11:43

回答

0

正如我在我的評論中提到:

Are you passing wab_id, Wab_id, or id to your server where you will access with $_GET? Because its not clear under which key you are passing it to yourself and if you are using the correct one - which may be the issue you are having right now.

你有那方遠操縱API PARAMS應該是一個簡單的事情經過:

$api = "http://foo.com/api/"; 
$params = array(
    'scope' => 'accepted', 
    'view' => 'films', 
); 

// you need to match the key you are using here to what you are passing 
// to your URL 
if (isset($_GET['id'])) { 
    $params['wab_id'] = $_GET['id']; 
} 

$url = $api . '?' . http_build_query($params); 

現在的最後一部分,你真的應該使用cURL, Http ...或者除file_get_contents之外的任何其他東西,因爲它並不能真正爲您提供處理可能遇到的錯誤的非常好的方法。我給你使用cURL一個簡單的例子:

$client = curl_init(); 
curl_setopt_array($client, array(
    CURLOPT_RETURNTRASNFER => true, 
    CURLOPT_URL => $url // the one we dynmically built above 
)); 

$responseText = curl_exec($client); 

if ($responseText !== false) { 
    $responseInfo = curl_getinfo($client); 

    if ($responseInfo['http_code'] === 200) { 
     // http status ok - you may need to take action based 
     // on other http status codes but im not going to delve into that here. 

     $data = json_decode($responseText, true); 
     print_r($data); 

    } else { 
     printf('Error accessing data: HTTP Status %s', $responseInfo['http_code']; 
    } 
} else { 
    printf('Error: (%s) %s', curl_errno($client), curl_error($client)); 
} 

curl_close($client); 
+0

我正在通過Wab_id – Charles 2015-02-11 01:07:45

+0

'client = curl_init(); curl_setopt_array($客戶端,陣列( CURLOPT_RETURNTRASNFER =>真, CURLOPT_URL => $網址 ));'返回curl_setopt_array的'的誤差():數組密鑰必須CURLOPT常量或等效的整數值的/ home/jewellsc /第20行的public_html/handler.php 錯誤:(3)沒有URL設置!'這可能很容易修復,但是除非是laravel,否則從未使用curl – Charles 2015-02-11 01:28:56