curl php非常慢

时间:2013-01-26 23:51:12

标签: 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_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);
}

3 个答案:

答案 0 :(得分:1)

你打开&在每次迭代中关闭http连接:)

使用curl_multi_init():

$chm = curl_multi_init();// <---

foreach ($tasks as $task) {
    $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_multi_add_handle($cmh, $ch);// <---
}

有关curl_multi_init()的更多信息,请参阅php.net

答案 1 :(得分:1)

我让你为curl multi请求提供这个函数,输入是一个带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;
    }

答案 2 :(得分:0)

每次打开新的卷曲会话时,设置相同的数据并关闭请求。通过声明在循环外没有变化的数据,可能会更快一些。

$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);