【发布时间】:2011-10-26 12:41:27
【问题描述】:
好的,我正在尝试使用 PHP 代理访问一些 JSON,因为有人告诉我,当您不控制站点策略时,这是进行跨域访问的唯一方法。
下面是我尝试用作 php 代理的代码,由 stackoverflow 用户共享:
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Set a referer
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
问题是当我将 $URL 替换为 http://www.nfl.com/liveupdate/scorestrip/ss.json 时,似乎什么也没发生。我也不确定如何使用这个 PHP 代理,因为我从来没有做过这种事情。
我想在单独的 php 文件中创建它,然后向该代码发送请求吗?我有点不知道到底该怎么做才能做到这一点,这样我就可以从上面的网站访问 json。
【问题讨论】:
标签: php json cross-domain