curl php非常慢
我构建了下面的脚本,以使用令牌创建facebook状态。问题是5个令牌的执行时间太长,需要>=10秒curl php非常慢,php,facebook-graph-api,curl,Php,Facebook Graph Api,Curl,我构建了下面的脚本,以使用令牌创建facebook状态。问题是5个令牌的执行时间太长,需要>=10秒 foreach ($tokens as $token) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/post_id/like'); curl_setopt($ch, CURLOPT_POST, true); curl_seto
foreach ($tokens as $token)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/post_id/like');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('access_token' => $token));
curl_exec($ch);
curl_close($ch);
}
每次打开新的curl会话时,设置相同的数据并发送请求。通过声明在循环外不会更改的数据,下面的操作可能会快一点
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/post_id/like');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101');
foreach ($tokens as $token) {
curl_setopt($ch, CURLOPT_POSTFIELDS, array('access_token' => $token));
curl_exec($ch);
}
curl_close($ch);
在每次迭代中打开并关闭http连接:) 使用curl\u multi\u init():
$chm=curl\u multi\u init();//$代币);
卷曲多加手柄($cmh,$ch);// 我让您了解curl multi-request的函数,输入是一个包含URL的数组,输出是一个包含结果的数组
对我来说很有帮助
对于您的问题,您只需要为令牌添加一个新的输入,并在curl请求中添加一个参数
function multiRequest($data, $options = array()) {
// array of curl handles
$curly = array();
// data to be returned
$result = array();
// multi handle
$mh = curl_multi_init();
// loop through $data and create curl handles
// then add them to the multi-handle
foreach ($data as $id => $d) {
$curly[$id] = curl_init();
$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
curl_setopt($curly[$id], CURLOPT_URL, $url);
curl_setopt($curly[$id], CURLOPT_HEADER, 0);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
//Set proxy type
// post?
if (is_array($d)) {
if (!empty($d['post'])) {
curl_setopt($curly[$id], CURLOPT_POST, 1);
curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
}
}
// extra options?
if (!empty($options)) {
curl_setopt_array($curly[$id], $options);
}
curl_multi_add_handle($mh, $curly[$id]);
}
// execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
// get content and remove handles
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
// all done
curl_multi_close($mh);
return $result;
}
使请求与多重查询并行进行。
function multiRequest($data, $options = array()) {
// array of curl handles
$curly = array();
// data to be returned
$result = array();
// multi handle
$mh = curl_multi_init();
// loop through $data and create curl handles
// then add them to the multi-handle
foreach ($data as $id => $d) {
$curly[$id] = curl_init();
$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
curl_setopt($curly[$id], CURLOPT_URL, $url);
curl_setopt($curly[$id], CURLOPT_HEADER, 0);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
//Set proxy type
// post?
if (is_array($d)) {
if (!empty($d['post'])) {
curl_setopt($curly[$id], CURLOPT_POST, 1);
curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
}
}
// extra options?
if (!empty($options)) {
curl_setopt_array($curly[$id], $options);
}
curl_multi_add_handle($mh, $curly[$id]);
}
// execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
// get content and remove handles
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
// all done
curl_multi_close($mh);
return $result;
}