How to use PHP CURL to bypass cross domain

2020-04-17 05:58发布

问题:

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.

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";
}
?>

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

Thanks

回答1:

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).

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);


回答2:

Using false in CURLOPT_RETURNTRANSFER doesn't return anything by curl. make it true(or 1)

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);