【问题标题】:Can't get url using JSON script parsed with file_get_contents无法使用用 file_get_contents 解析的 JSON 脚本获取 url
【发布时间】:2014-03-27 23:53:17
【问题描述】:

我有这个链接,我想解析其中的一些信息,或者只是将它保存在一个文件中...

没有这个简单的代码就做不到:

示例:

<?php
$myFile = 'test.txt'; 
$get= file_get_contents("http://www.ticketmaster.com/json/resale?command=get_resale_listings&event_id=0C004B290BF2D95F");
file_put_contents($myFile, $get); ?>

输出是:

{"version":1.1,"error":{"invalid":{"cookies":true}},"command":"get_resale_listings"}

我尝试了许多其他方法,例如 fopen 或 include 也不起作用。我不明白,因为当我将 url 放入浏览器时,它会准确显示所有代码(谷歌浏览器),或者最好让我将其保存为文件(资源管理器)。看起来像浏览器 cookie 或我的本地主机上没有加载的东西??

感谢您的提示。

【问题讨论】:

  • 你可以看看这个问题:stackoverflow.com/questions/3938534/…,但正如它在 cmets 中所说,它可能是 php.ini 设置:allow_fopen_url 设置为 off,这会破坏代码。
  • 谢谢,但 allow_fopen_url 已打开,也不起作用。

标签: php json file-get-contents


【解决方案1】:

您需要使用 CURL 访问该网址。

服务器检查客户端是否启用了 cookie。使用 file_get_content() 您不会发送任何有关客户端(浏览器)的信息。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.ticketmaster.com/json/resale?command=get_resale_listings&event_id=0C004B290BF2D95F');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_exec($ch);

【讨论】:

  • 好的...但是 cookie 位于我浏览器的内容设置中,我怎样才能将它们放入 my_cookies.txt 中?
猜你喜欢
  • 1970-01-01
  • 2021-08-08
  • 1970-01-01
  • 2016-02-23
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多