简体   繁体   中英

curl_init is enabled but not working

I am trying to open a url using curl_init, but didn't get success to get the right responce. I am not able to share the exact url with all of you because of security reasons.

Below is my code

$ch = curl_init("Site URL"); 
print_r($ch);
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_POST, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch); 

echo "Below is Responce <br/>";
echo $response;
print_r($response); 

Below is the output

Resource id #2Below is Responce 

Output is not throwing any error even if i used error_reporting(1) and report error is enabled in my webhosting php setting

I also checked error logs but nothing. Could you please help me to find the cause. Above code is working from different servers but not from my actual production server. Please guide me to find the cause.

Here's a working code:

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://xyz.curlinittest.php");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    $data = curl_exec($ch);
    echo $data;
    curl_close($ch);
?>

Output: Hello World

Your third-party server was rejecting queries based on user-agent. Set curl to act as if it was a firefox and .. tadaaa

So my code is correct so no need to provide an resolution for the same. Actually i raised the TKT with my hosting provider. As per the communication with Hosting Provider they confirm it is a server issue due to blocking of Port 80. I hope this will help others. So Port blocking is also an issue if your Curl_init is not working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM