我试图在这里收到json文件的内容,但是当我想回显输出($json)时它没有给我任何东西.我在stackoverflow上看了一些涉及cUrl的其他问题,并使用了答案中给出的cUrl设置,所以它应该工作正常.难道我做错了什么?这是我的代码:
$url = 'http://steamcommunity.com/profiles/<your 64-bit steam ID>/inventory/json/730/2';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$json = json_decode($output, true);
print_r($json);
最佳答案 在$output = curl_exec($ch)之后添加;线:
$info = curl_getinfo($ch);
echo "<pre>";
print_r($info);
echo "</pre>";
请在此打印您在浏览器中看到的内容.没有这些信息,人们无法帮助你.
我运行你的代码,它的工作正常.我有HTTP答案代码200和页面加载成功.页面说:“无法找到指定的配置文件”(没关系,因为我没有密钥).
我的$info打印:
Array
(
[url] => http://steamcommunity.com/profiles//inventory/json/730/2
[content_type] => text/html; charset=UTF-8
[http_code] => 200
[header_size] => 927
[request_size] => 109
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.561
[namelookup_time] => 0.062
[connect_time] => 0.109
[pretransfer_time] => 0.109
[size_upload] => 0
[size_download] => 18266
[speed_download] => 32559
[speed_upload] => 0
[download_content_length] => 18266
[upload_content_length] => -1
[starttransfer_time] => 0.561
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 2.17.165.89
[certinfo] => Array
(
)
[primary_port] => 80
[local_ip] => 192.168.1.33
[local_port] => 53366
)
一切都很完美.也许您与steamcommunity.com的联系不佳?如果你收到[http_code] => 0意味着是的.
如果添加这个你看到了什么:
print_r($output);
可能是页面加载,但不是JSON?如果是的话,你的$json应该是空的,这绝对没问题.
您的评论后更新:
您收到了302个HTTP代码和重定向URL.您需要转到此URL,您将收到JSON.完整代码:
$url = 'http://steamcommunity.com/profiles/<your 64-bit steam ID>/inventory/json/730/2';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_setopt($ch, CURLOPT_URL, $info["redirect_url"]); // Set new URL
$output = curl_exec($ch); // Go to new URL
$json = json_decode($output, true); // Your JSON here, I checked it
print_r($json); // Print JSON