1
我在個人項目上有點迷茫,並尋求一些幫助。我發現了一個cURL代碼,可以讓Google+記錄一個特定的URL,並且在對您試圖獲取的網址進行硬編碼時效果很好。我正嘗試使用GET基於表單數據運行此腳本。請參閱下面的工作和非工作示例。我也加粗了我正在改變的代碼行。PHP變量與cURL混合
WORKS
*$url = "http://mashable.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$curl_results = curl_exec ($ch);
curl_close ($ch);
$parsed_results = json_decode($curl_results, true);
echo $parsed_results[0]['result']['metadata']['globalCounts']['count'];
不起作用
$url = $_GET['url'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$curl_results = curl_exec ($ch);
curl_close ($ch);
$parsed_results = json_decode($curl_results, true);
echo $parsed_results[0]['result']['metadata']['globalCounts']['count'];
'$ _GET ['url']'的內容是什麼?在'curl_setopt()'中使用'urlencode($ url)'' – 2012-07-17 20:51:39
$ _GET ['url'] =完全相同的http://mashable.com。我試過,urlencode,它沒有任何區別。 – user1532904 2012-07-17 21:04:01
他們是_exactly_相同,如果你'var_dump($ _ GET ['url'])'? – 2012-07-17 21:12:02