PHP:file_get_contents & curl 不起作用(但终端 curl 可以)



我正在尝试从此链接中卷曲json:http://web-app.usc.edu/ws/soc/api/departments/20131

我试过:

file_get_contents:

$json = file_get_contents("http://web-app.usc.edu/ws/soc/api/departments/20131");

卷曲:

$ch = curl_init('http://web-app.usc.edu/ws/soc/api/departments/20131');
$response = curl_exec($ch);

但file_get_contents"无法打开溪流"和卷曲似乎只是挂起来。有趣的是,在终端中卷曲 url 工作得很好。

出了

什么问题?

Php 设置可以(并且应该)禁止在 url 上使用调用 fopen 的 file_get_contents。 请参阅 http://www.php.net/manual/en/filesystem.configuration.php。

卷曲是抓取文件的正确方法。 我认为您的问题可能是缺少卷曲选项。尝试将CURLOPT_RETURNTRANSFER设置为 true。 如果不这样做,则直接输出传输,而不是调用 curl_exec() 的返回值。

$ch = curl_init('http://web-app.usc.edu/ws/soc/api/departments/20131');
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
$response = curl_exec($ch);

另一个RTM链接:http://www.php.net/manual/en/function.curl-setopt.php

最新更新