php - How do I fetch Google Plus share count using cURL multi? -
i found following function i've been able use collect , cache share counts on various social networks. far, can feed twitter, linkedin, facebook, , pinterest url's in array function , kick response can parse , count from.
in effort speed process, found process uses curl multi fetch shares @ same time instead of processing 1 request @ time.
however, curl had been using google plus has lot more configuration in order make work. possible configuration function it's looping through requests, if sees google plus, adds of information specific request, still runs request simultaneously others?
here's curl multi function i'm using:
function sw_fetch_shares_via_curl_multi($data, $options = array()) { // array of curl handles $curly = array(); // data returned $result = array(); // multi handle $mh = curl_multi_init(); // loop through $data , create curl handles // add them multi-handle foreach ($data $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']); } } // options? if (!empty($options)) { curl_setopt_array($curly[$id], $options); } curl_multi_add_handle($mh, $curly[$id]); } // execute handles $running = null; { curl_multi_exec($mh, $running); } while($running > 0); // content , remove handles foreach($curly $id => $c) { $result[$id] = curl_multi_getcontent($c); curl_multi_remove_handle($mh, $c); } // done curl_multi_close($mh); return $result; }
the array feed this:
$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;
and on , forth other networks. pass curl multi function, , send me json can parse , work with.
here's configuration google plus integrate same function can pass in well:
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; }
can set loop somehow see if $id 'googleplus', adds stuff particular request? possible check if it's google plus right before curl_setopts lines , pass these other ones in instead somehow? thanks.
it turns out lot easier thought:
function sw_fetch_shares_via_curl_multi($data, $options = array()) { // array of curl handles $curly = array(); // data returned $result = array(); // multi handle $mh = curl_multi_init(); // loop through $data , create curl handles // add them multi-handle foreach ($data $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; // options? if (!empty($options)) { curl_setopt_array($curly[$id], $options); } curl_multi_add_handle($mh, $curly[$id]); } // execute handles $running = null; { curl_multi_exec($mh, $running); } while($running > 0); // content , remove handles foreach($curly $id => $c) { $result[$id] = curl_multi_getcontent($c); curl_multi_remove_handle($mh, $c); } // done curl_multi_close($mh); return $result; }
you can use function fetch 5 major networks using simultaneous connections instead of 1 @ time.
for array, other networks require request url, google plus has it's request information built plugin requires url of page want information for.
Comments
Post a Comment