大家好,我需要一些基于网站网址编辑API范围的帮助,所以如果我有foo.com/?Wab_id=15,它会将API范围编辑为
?scope=accepted&view=films&wab_id=15
我以为我可以拥有像
这样的东西$ApiData = file_get_contents('http://foo.com/api/?scope=accepted&view=films/&wab_id=$id');
然后使用Get来检索传入url的id以编辑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()
我还尝试使用foreach内部的foreach循环遍历Muti数组,并显示代码和结果在下面的值
$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 :(得分:0)
正如我在评论中提到的那样:
您是否将wab_id,Wab_id或id传递到您将使用$ _GET访问的服务器?因为不清楚你把它传递给自己,以及你使用的是正确的 - 这可能是你现在遇到的问题。
在你完成操作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);