php - 使用 cUrl 时无输出

标签 php curl

我试图在这里接收一个 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 并且页面加载成功。 Page 说:“找不到指定的配置文件”(没关系,因为我没有 key )。

我的 $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

关于php - 使用 cUrl 时无输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34907373/

相关文章:

php - WooCommerce 产品 : Wrong product permalink in custom loop

python - 在 Flask 中访问传入的 POST 数据

php - IS NULL 在列中找不到空行

php - 如何使用 laravel 将一个表的内容传输到另一个数据库表

php - 使用 Tumblr PHP API 时出现 fatal error cURL 错误 60

python - 什么是 python 'requests' api 等效于我的 shell 脚本 curl 命令?

bash - 如何从命令行将多张图片上传到 imgur 相册?

python - 如何使用 python 子进程从 curl 检索信息

php - 如何在所有php文件中使用用于登录的用户名

php - 进行 CodeIgniter 数据库查询