如何使用PHP CURL绕过跨域 [英] How to use PHP CURL to bypass cross domain

查看:493
本文介绍了如何使用PHP CURL绕过跨域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要PHP提交参数从一个域到另一个域。 JavaScript不是我的情况的选择。我现在尝试使用CURL与PHP,但是没有成功绕过跨域。

I need PHP to submit paramaters from one domain to another. JavaScript is not an option for my situation. I'm now trying to use CURL with PHP, but have not been successful in bypassing the cross domain.

从domain_A,我有一个页面与下面的PHP与CURL脚本:


From domain_A, I have a page with the following PHP with CURL script:

if (_iscurl()){
    echo "<p>CURL is enabled</p>";
    $url = "http://domain_B/process.php?id=123&amt=100&jsonp=?";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,10);
    curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt($ch, CURLOPT_URL, $url );
    $return = curl_exec($ch);
    curl_close($ch);

    echo "<p>Finished operations</p>";
}
else{
    echo "CURL is disabled";
}
?>

我没有得到任何结果,所以我假设P​​HP CURL脚本不成功。有任何解决方法吗?

I am not getting any results, so I am assuming that the PHP CURL script is not successful. Any ideas to fix this?

感谢

推荐答案

位晚。但添加这个答案为更多的读者谁可能面临类似的问题。当我们从通过 http 托管的域发送php curl请求到通过 https(http over ssl)托管的域时,会出现此问题。

Well, its bit late. But adding this answer for further readers who might face similar issue. This issue arises some times when we are sending php curl request from a domain hosted over http to a domain hosted over https (http over ssl).

只需在执行curl之前添加以下代码段。

Just add below code snippet before curl execution.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);

这篇关于如何使用PHP CURL绕过跨域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆