0

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.

  • perhaps you forgot CURLOPT_SSL_VERIFYHOST to false – Sam Jan 29 '16 at 12:06
  • If it works on some computers and not yours it probably is an issue of configuring. I had a similar issue where i didn't get any specific errors to handle the issues but a solution for this is to use VERBOSE to get the errors and issues. This thread helped me with this: http://stackoverflow.com/questions/3757071/php-debugging-curl – Martin Jan 29 '16 at 12:10
  • Maybe curl is disabled in your php.ini Are you trying this script localy or on a remote host ? – Math Jan 29 '16 at 12:11
  • Add below lines to beginning of your file:- error_reporting(E_ALL); ini_set("display_errors", 1); – Ravi Hirani Jan 29 '16 at 12:20
  • @Mathieu : Remote Host and it is enabled cURL support enabled cURL Information 7.36.0 Age 3 Features AsynchDNS Yes Debug No GSS-Negotiate Yes IDN Yes IPv6 Yes Largefile Yes NTLM Yes SPNEGO No SSL Yes SSPI No – Mayank Sanghvi - vLemomm Jan 29 '16 at 12:22
  • No luck Below is the updated code "; echo $response; print_r($response); ?> – Mayank Sanghvi - vLemomm Jan 29 '16 at 12:28
  • @MayankSanghvi: Please add this line: `echo 'Curl error: ' . curl_error($ch);` at last line. Let me know what is the error print here. – AddWeb Solution Pvt Ltd Jan 29 '16 at 13:11

2 Answers2

2

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

Math
  • 666
  • 8
  • 26
  • Thanks for your code. Below is the output Resource id #1There is an error !. I used this URL to test http://rosebi.com/curlinittest.php – Mayank Sanghvi - vLemomm Jan 29 '16 at 12:37
  • Actually we need to use third party services, which hosted on some other servers. In their code the provide curl_init() . In output they have success and failure. Previously our code is working upto 26 Jan 2016. Now i don't know why it is stopped working. Also the working code execution time is in fraction of seconds. Now the same script taking arround 15 to 40 secs – Mayank Sanghvi - vLemomm Jan 29 '16 at 12:42
  • On Success the script trigger a SMS :-(. Now i need to find a workaround. – Mayank Sanghvi - vLemomm Jan 29 '16 at 12:50
  • I am trying to figure the meaning of output print_r($ch); line which is Resource id #2 – Mayank Sanghvi - vLemomm Jan 29 '16 at 13:02
  • I think it means that it's an array of 2 things. Try var_dump($ch) in place of print_r() – Math Jan 29 '16 at 13:06
  • Is their alternative of curl_init ? My code is working fine on my another production server. – Mayank Sanghvi - vLemomm Jan 29 '16 at 13:20
  • If you just need to get content from a webpage, you can use php's file_get_contents() http://php.net/manual/fr/function.file-get-contents.php – Math Jan 29 '16 at 13:22
  • I updated my answer, i managed to have curl outputing the content of the page, i bypassed 403 error by setting curl user-agent – Math Jan 29 '16 at 13:35
0

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.