Javascript 用php中的本地代理替换whateverorigin.org调用
我目前在一些javascript中使用whateverorigin.org来检索作为JSON对象的URL,因为第三方站点没有通过JSON API提供它们的一个函数 我想从我的网站上删除这种依赖性,因为whateverorigin.org破坏了HTTPS/SSL浏览器对安全内容的检查,因为这是一个明确的http调用 有人这样做过吗?我在任何地方都找不到这样的例子Javascript 用php中的本地代理替换whateverorigin.org调用,javascript,php,json,ssl,Javascript,Php,Json,Ssl,我目前在一些javascript中使用whateverorigin.org来检索作为JSON对象的URL,因为第三方站点没有通过JSON API提供它们的一个函数 我想从我的网站上删除这种依赖性,因为whateverorigin.org破坏了HTTPS/SSL浏览器对安全内容的检查,因为这是一个明确的http调用 有人这样做过吗?我在任何地方都找不到这样的例子 提前感谢您的回复 好的,自从我第一次输入这个问题以来,我已经找到了一些例子,并在php中拼凑出了一个可用的代理函数。。。请随意使用它为您
提前感谢您的回复 好的,自从我第一次输入这个问题以来,我已经找到了一些例子,并在php中拼凑出了一个可用的代理函数。。。请随意使用它为您自己的目的
<?php
// Sourced from: http://stackoverflow.com/questions/2511410/curl-follow-location-error
function curl_exec_follow(/*resource*/ &$ch, /*int*/ $redirects = 20, /*bool*/ $curlopt_header = false) {
if ((!ini_get('open_basedir') && !ini_get('safe_mode')) || $redirects < 1) {
curl_setopt($ch, CURLOPT_HEADER, $curlopt_header);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $redirects > 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, $redirects);
return curl_exec($ch);
} else {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FORBID_REUSE, false);
do {
$data = curl_exec($ch);
if (curl_errno($ch))
break;
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != 301 && $code != 302)
break;
$header_start = strpos($data, "\r\n")+2;
$headers = substr($data, $header_start, strpos($data,"\r\n\r\n", $header_start)+2-$header_start);
if (!preg_match("!\r\n(?:Location|URI): *(.*?) *\r\n!",$headers, $matches))
break;
curl_setopt($ch, CURLOPT_URL, $matches[1]);
} while (--$redirects);
if (!$redirects)
trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING);
if (!$curlopt_header)
$data = substr($data, strpos($data, "\r\n\r\n")+4);
return $data;
}
}
header('Content-Type: application/json');
$retrieveurl = curl_init(urldecode($_GET['url']));
$callbackname = $_GET['callback'];
$htmldata = curl_exec_follow($retrieveurl);
if (curl_error($retrieveurl))
die(curl_error($retrieveurl));
$status = curl_getinfo($retrieveurl, CURLINFO_HTTP_CODE);
curl_close($retrieveurl);
$data = array('contents' => $htmldata, 'status' => $status);
$jsonresult = json_encode($data);
echo $callbackname . '(' . $jsonresult . ')';
?>
希望这对别人有帮助