[英]Using cURL to read JSON data
使用Chrome瀏覽器檢查網址: http : //www.wizards.com/Magic/PlaneswalkerPoints/76954206#我能夠看到從以下位置獲取JSON數據的網址:
要求網址: http : //www.wizards.com/Magic/PlaneswalkerPoints/JavaScript/GetPointsSummary/76954206
運行我的代碼:
$user_agent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36';
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // times out after 5s
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($json)));
curl_setopt($ch, CURLOPT_POST, 1);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// further processing ....
if ($server_output == "OK") {
print_r($ch);
} else {
echo "Boo";
}
給我留白頁。 它不返回JSON,僅返回重定向錯誤頁面。
我是否缺少某個電話來獲取JSON數據?
嘗試這個:
<?php
$url = 'http://www.wizards.com/Magic/PlaneswalkerPoints/JavaScript/GetPointsSummary/76954206';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS, '');
$result = curl_exec($ch);
curl_close($ch);
echo $result;
輸出:
{ “結果”:真實的, “sessionExpired時”:假的, “數據”:[{ “關鍵”: “LifetimePoints”, “值”:6016,“ReplaceElement ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.