编辑 API URL 的范围
Editing the scope of an API URL
大家好,我需要一些帮助来编辑基于网站 url 的 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 然后调用数组内的 a 键但也没有得到太多运气我的代码在下面
$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>";
}
?>
但是这个 returns 是
的错误
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 ->
正如我在评论中提到的:
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 参数应该是一件简单的事情:
$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);
大家好,我需要一些帮助来编辑基于网站 url 的 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 然后调用数组内的 a 键但也没有得到太多运气我的代码在下面
$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>";
}
?>
但是这个 returns 是
的错误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 ->
正如我在评论中提到的:
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 参数应该是一件简单的事情:
$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);