我使用Rolling cUrl PHP库从带有JSON的API中获取数据。
在Rolling CURL库文档中,我尝试了以下示例,该示例效果很好:
$rollingCurl = new \RollingCurl\RollingCurl();
$rollingCurl
->get('https://www.myapi.com')
->get('https://www.myapi2.com')
->setCallback(function(\RollingCurl\Request $request, \RollingCurl\RollingCurl $rollingCurl) {
$output = json_decode($request->getResponseText());
$data[$x++] = $output; //How do I save it outside this function?
})
->setSimultaneousLimit(3)
->execute();
//Can I get the content of each API call without the callback?
print_r($data); // returns nothing :(
我尝试根据自己的需要更改它,并保存每个"刮擦"在一个数组中,但我找不到从回调或其外部保存数据的方法。
这就是我的尝试:
date_added
所以我的问题是:
如何在此功能之外保存数据阵列?
我可以在没有回调的情况下(执行后)获取每个API调用的内容吗?
答案 0 :(得分:1)
我认为在回调Closure中初始化$data
和use
应该可以解决您的问题。如果没有这个,$data
数组只在Closure的范围内设置(请注意use (&$data)
声明你的回调函数时):
/** IMPORTANT **/
$data = array();
/***************/
$rollingCurl = new \RollingCurl\RollingCurl();
$rollingCurl
->get('https://www.myapi.com')
->get('https://www.myapi2.com')
->setCallback(function(\RollingCurl\Request $request, \RollingCurl\RollingCurl $rollingCurl) use (&$data) {
$output = json_decode($request->getResponseText());
$data[$x++] = $output;
})
->setSimultaneousLimit(3)
->execute();
// Yes, you can!
print_r($data);
我喜欢的另一个不错的功能是Closure范围绑定,只是一个快速且未经测试的示例(如果您需要更多内容,请告诉我:)):
class CurlHandler {
$data = array();
public function processOutput($data) {
// Do something with the data retrieved here
$this->data[] = $data;
}
public function getData() { return $this->data; }
}
$curlResponseHandler = new CurlHandler();
// Your callback to be passed into \RollingCurl\RollingCurl::setCallback()
$closure = function(\RollingCurl\Request $request, \RollingCurl\RollingCurl $rollingCurl) {
$output = json_decode($request->getResponseText());
$this->processOutput($output);
}
// Bind your callback Closure to your CurlHandler scope
// => the scope binding makes $this inside your callback Closure refer to your CurlHandler instance
$closure->bindTo($curlResponseHandler);
$rollingCurl = new \RollingCurl\RollingCurl();
$rollingCurl
->get('https://www.myapi.com')
->get('https://www.myapi2.com')
->setCallback($closure)
->setSimultaneousLimit(3)
->execute();
// Get all your outputs from your CurlHandler class now:
print_r($curlResponseHandler->getData());
或者最后但并非最不重要的只是直接在你的CurlHandler类上构建一个回调,在这种情况下可能是我的最爱:
class CurlHandler {
$data = array();
public function processOutput(\RollingCurl\Request $request, \RollingCurl\RollingCurl $rollingCurl) {
$output = json_decode($request->getResponseText());
$this->data[] = $output;
}
public function getData() { return $this->data; }
}
$curlResponseHandler = new CurlHandler();
$rollingCurl = new \RollingCurl\RollingCurl();
$rollingCurl
->get('https://www.myapi.com')
->get('https://www.myapi2.com')
->setCallback(array($curlResponseHandler, 'processOutput'))
->setSimultaneousLimit(3)
->execute();
// Get all your outputs from your CurlHandler class now:
print_r($curlResponseHandler->getData());
最后一个选项假定setCallback()
一般接受callables而不仅仅Closure
s。
希望有所帮助,欢呼!