【问题标题】:Getting json data from a webpage using PHP使用 PHP 从网页获取 json 数据
【发布时间】:2015-08-20 22:30:39
【问题描述】:

我正在尝试从 here (example url) 获取响应,首先,我认为我应该使用 file_get_contents()

当我尝试这个时,我得到了以下错误:

Warning: file_get_contents(https://steamcommunity.com/market/pricehistory/?country=US&currency=1&appid=730&market_hash_name=SG%20553%20|%20Damascus%20Steel%20(Factory%20New)): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request

我知道这是因为它将 & 转换为 &。我尝试了很多方法来解决这个问题,但是它们都失败了,在快速谷歌之后,我得出的结论是 file_get_contents() 会自动将 & 转换为 &

我的下一步是尝试 curl。我首先尝试了以下代码:

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
   CURLOPT_RETURNTRANSFER => 1,
   CURLOPT_URL => 'http://steamcommunity.com/market/pricehistory/?country=US&currency=1&appid=730&market_hash_name='.$hash,
   CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.2 (KHTML, like Gecko) ChromePlus/4.0.222.3 Chrome/4.0.222.3 Safari/532.2'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

但这返回了‹ŠŽÿÿ)»L 作为响应。我想知道这是否与 json 编码有关,所以我尝试将它通过 json_decode() 但它不起作用。

接下来,我尝试了:

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://steamcommunity.com/market/pricehistory/',
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.2 (KHTML, like Gecko) ChromePlus/4.0.222.3 Chrome/4.0.222.3 Safari/532.2',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        country => "US",
        currency => 1,
        appid => 730,
        market_hash_name => "SG%20553%20|%20Damascus%20Steel%20(Factory%20New)"
    )
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

但又得到了回复‹ŠŽÿÿ)»L

这个响应是什么意思,我可以解析它吗?如果没有,我应该如何正确获取这些数据?此外,为什么 file_get_contents() 不起作用?

【问题讨论】:

  • 您是否需要某种类型的访问令牌才能访问 Steam 网络 API?
  • 事实证明,您必须登录 Steam 才能访问价格概览 - 这与大多数 API 不同,后者需要 API 密钥

标签: php json curl steam-web-api


【解决方案1】:

我很确定会发生这种情况,因为您需要某种类型的访问令牌才能访问 Steam 网络 API。

请参阅 SO 上的 this answer

基本上,Steam 正在返回一个带有“400 Bad Request”状态的错误。但是,可以通过以下方式忽略此错误:

<?php
    $url = "https://steamcommunity.com/market/pricehistory/?country=US&currency=1&appid=730&market_hash_name=SG%20553%20%7C%20Damascus%20Steel%20(Factory%20New)";
    $context = stream_context_create(array(
      'http' => array(
          'ignore_errors'=>true,
          'method'=>'GET'
          // for more options check http://www.php.net/manual/en/context.http.php
        )
    ));
    $response = file_get_contents($url, false, $context);
    echo $response; // returns "[]"
?>

请务必查看 SO 上的 this answer

【讨论】:

  • 正常的 web api 请求是这样,但 Steam 市场请求不是(尝试访问浏览器中的链接)
  • 那么我不确定为什么 Steam 会返回“400 Bad Request”错误,但无论哪种方式,都可以通过将 ignore_errors 设置为 true 来忽略该错误,这似乎可以解决问题.
  • 它返回[],你是对的。然而,网页远不止于此。有什么想法吗?
  • 是吗?在我的浏览器中查看steamcommunity.com/market/pricehistory/…,我只得到[](PHP 也返回)。
  • 是的,请参阅my answer,似乎 Steam 要求您登录,我是,所以我看到的页面与您完全不同
【解决方案2】:

可能你的响应是 gzip,尝试使用 CURLOPT_ENCODING。

curl_setopt($curl ,CURLOPT_ENCODING, '')

如果您使用 https,请不要忘记禁用 CURLOPT_SSL_VERIFYPEER。

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false)

有一件事,如果我用浏览器点击您的链接并打开我的调试控制台。 我看到您请求有 400 状态代码(错误请求)。

【讨论】:

    【解决方案3】:

    我不能说你的 enpoint,但你可以通过使用 urlencode() 来解决你的错误请求错误:

    $url = urlencode('https://steamcommunity.com/market/pricehistory/?country=US&currency=1&appid=730&market_hash_name=SG%20553%20%7C%20Damascus%20Steel%20(Factory%20New))'
    file_get_contencts($url); 
    

    【讨论】:

    • 我已经尝试过了,也没有任何问题。正确的东西已经被编码了,取决于你编码的其他内容,你会得到和我一样的错误,或者 php 无法识别 URL
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 2020-02-22
    相关资源
    最近更新 更多