在 php 中 whateverorigin.org 调用替换为本地代理
Replace whateverorigin.org call with local proxy in php
我目前正在一些javascript中使用 whateverorigin.org 来检索URL作为JSON对象,因为第三方站点没有通过其JSON API提供其功能之一。
我想从我的网站中删除此依赖项,因为 whateverorigin.org 破坏了 HTTPS/SSL 浏览器对安全内容的检查,因为它是一个明确的 http 调用。
有人这样做过吗?我在任何地方都没有找到它的例子。
提前感谢您的回复!
好的,所以自从我第一次输入这个问题以来,我已经找到了一些示例,并在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 . ')';
?>
希望这对某人有所帮助!
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 我如何找到一个句子中的所有空格并替换忽略它们
- 如何用更合适的内容替换document.write
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- 在不打开聊天屏幕的情况下制作Zopim-ding代理
- 在DOM中查找一个模式并替换它's的内容使用jquery
- 用空格替换下划线PHP
- str.split(someString).join(someOtherString)是否等效于替换
- 使用javascript的图像替换循环
- window.location替换并传递URL历史记录条目中的变量
- 替换标记中的属性
- 指令的模板必须只有一个根元素:With restrict E&替换true
- AngularJs对ng消息的自定义替换
- 用超链接替换URLS
- 用cdata标记替换脚本标记
- Javascript对象类在单击时打开窗口进行颜色选择,并在更改时替换对象背景颜色
- 在 php 中 whateverorigin.org 调用替换为本地代理
- 如何在检测到用户代理后使用jquery替换css
- JQuery将图像替换为Base64(html2canvas无代理)
- 用我自己的替换AngularJS或BreezeJS ajax对象来允许服务器端代理