2015-02-10 54 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