我一直在关注如何使用curl_multi
. http://arguments.callee.info/2010/02/21/multiple-curl-requests-with-php/
我不知道我做错了什么,但curl_multi_getcontent
返回 null。假设返回 JSON。我知道这不是 mysql 调用,因为我使用了 while 循环和 standard curl_exec
,但是页面加载时间太长。(为了安全起见,我更改了一些 setopt 细节)
相关的 PHP 代码片段。最后我确实关闭了while循环。
$i = 0;
$ch = array();
$mh = curl_multi_init();
while($row = $result->fetch_object()){
$ch[$i] = curl_init();
curl_setopt($ch[$i], CURLOPT_CAINFO, 'cacert.pem');
curl_setopt($ch[$i], CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch[$i], CURLOPT_URL, 'https://mysite.com/search/'.$row->username.'/');
curl_multi_add_handle($mh, $ch[$i]);
$i++;
}
$running = 0;
do {
curl_multi_exec($mh, $running);
} while ($running > 0);
$result->data_seek(0);
$i = 0;
while ($row = $result->fetch_object()) {
$data = curl_multi_getcontent($ch[$i]);
$json_data = json_decode($data);
var_dump($json_data);
编辑
这是当前有效的代码,但会导致页面加载速度过慢
$ch = curl_init();
curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem');
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
while($row = $result->fetch_object()){
curl_setopt($ch, CURLOPT_URL, 'https://mysite.com/search/'.$row->username.'/');
$data = curl_exec($ch);
$json_data = json_decode($data);
var_dump($json_data);
}