我正在尝试使用Yelp API。为了获取我需要的信息,我首先需要致电Yelp搜索API,提取场地的ID,并使用ID来调用Yelp Business API以深入到我需要的信息。
当我在使用curl_multi的许多API调用中,因此我需要在数组中发送数据。我是第一个呼叫Yelp搜索API,并且可以看到返回的ID。但是,然后我试图将这些ID放入新的数组中,以在第二个curl_multi中使用Yelp Business API。我正在尝试使用array_push,但我无法使其工作。
这是我的代码:
(EDIT)//A for loop to get the first 20 results
for ($i = 0; $i < 20; $i++) {
// $results contains this search results as an associative array
$results = reset(json_decode(curl_multi_getcontent($ch[$i]), true));
//I set up 2 arrays, the $idArray to use in the 2nd API call and $testNull
$idArray = array();
$testNull = array();
//the JSON response is decoded previously and here I go through the first 20 results
for($j = 0; $j < 20; $j++){
//Here I put the ID of each venue into $busID
$busID = $results[$j]['id'];
//In case the result is null I have set up this if statement
if(!empty($busID)){
//This is echoing out the IDs of the venues correctly
echo $busID;
//and here the problem lies - just can't get the IDs in $idArray
array_push($idArray, $busID);
}else{
//I set this up just to test NUll responses from the 1st API call.
array_push($testNull, $busID);
}
}
}
var_dump($idArray);
var_dump($testNull);
正如我在评论中所说的那样,回应$ busID确实确实给了我第1 Yelp API呼叫中的ID,但是$ idarray的Var_dump只是返回具有20个null值的数组。
任何人都可以放光吗?
谢谢
在另一个循环中,您仍然会看到vardumps。这部分代码工作...请参阅此沙盒链接:
http://sandbox.onlinephpfunctions.com/code/ddc4084713ae8ac15783ac653466524556b4169a
但是在另一个循环中,您的$ idarray可能被重置为空。
您确实意识到您的$idArray
对象是在每个新'迭代'上创建的。也许最后一次迭代具有无效的值?var_dump
仅打印最后一个数组对象(循环完成后) - 因为转储在循环
移动阵列:
$idArray = array();
$testNull = array();
在循环的顶部So:
$idArray = array();
$testNull = array();
for ($i = 0; $i < 20; $i++) {
....
}