0

我使用令牌构建以下脚本来喜欢 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);
}
4

3 回答 3

1

我让你使用 curl 多请求的函数,输入是一个带有 urls 的数组,输出是一个带有结果的数组。

对我来说很有帮助。

对于您的问题,您只需为令牌添加一个新输入并在 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;
    }
于 2013-01-27T00:32:20.913 回答
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上看到

于 2013-01-27T00:06:09.673 回答
0

每次您打开一个新的 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);
于 2013-01-27T00:04:29.800 回答