如何使用cURL multi获取Google Plus的共享计数?

时间:2015-08-07 21:42:07

标签: php curl

我找到了以下功能,我可以使用它来收集和缓存各种社交网络上的共享计数。到目前为止,我可以将一个数组中的Twitter,LinkedIn,Facebook和Pinterest URL提供给这个函数,他们都会回复我可以解析并从中获取计数的响应。

为了加快这个过程,我最近发现这个过程使用cURL multi来同时获取所有共享,而不是一次处理一个请求。

但是,我为Google Plus使用的cURL有更多配置才能使其正常运行。是否可以将此配置添加到此功能中,以便在循环请求时,如果它看到Google Plus,它会将所有这些信息添加到该特定请求中,但仍会同时向其他请求运行请求?

这是我正在使用的cURL多功能:

function sw_fetch_shares_via_curl_multi($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);

    // 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;
}

我输入的数组基本上是这样的:

$request_url['pinterest'] = 'https://api.pinterest.com/v1/urls/count.json?url='.$url;
$request_url['twitter'] = 'https://urls.api.twitter.com/1/urls/count.json?url=' . $url;

依旧等等其他网络。我将它们传递给cURL多功能,然后他们发给我一些我可以解析并使用的json。

以下是Google Plus的配置,我希望将其集成到同一个功能中,以便我可以轻松地将其传入:

function sw_fetch_googlePlus_shares($url)  {
    $url = rawurlencode($url);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'.rawurldecode($url).'","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
    $curl_results = curl_exec ($curl);
    curl_close ($curl);
    $json = json_decode($curl_results, true);
    return isset($json[0]['result']['metadata']['globalCounts']['count'])?intval( $json[0]['result']['metadata']['globalCounts']['count'] ):0;
}

我能以某种方式设置该循环以查看$ id是否为'googlePlus',然后将所有这些内容添加到该特定请求中?是否有可能在curl_setopts行之前检查它是否是Google Plus,然后以某种方式传递这些其他的?感谢。

1 个答案:

答案 0 :(得分:0)

事实证明,这比我想象的容易得多:

function sw_fetch_shares_via_curl_multi($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();

    if($id == 'googlePlus'):

        curl_setopt($curly[$id], CURLOPT_URL, "https://clients6.google.com/rpc");
        curl_setopt($curly[$id], CURLOPT_POST, true);
        curl_setopt($curly[$id], CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curly[$id], CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'.rawurldecode($d).'","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
        curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curly[$id], CURLOPT_HTTPHEADER, array('Content-type: application/json'));

    else:    

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

    endif;


    // 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;
}

您现在可以使用此功能使用同时连接而不是一次连接来获取所有五个主要网络。

对于阵列,所有其他网络都需要请求网址,但Google Plus会在该插件中内置其请求信息,因此它只需要您需要信息的网页的网址。