cURL not getting response from server?

Hawk27 picture Hawk27 · Jan 14, 2014 · Viewed 12.9k times · Source

I am trying to use cURL to get a response from a different server after it executes a SSH command. When doing so, I thought it was a connection error because I was not receiving any response from the server. So I made a test file and it outputs "Works!", but cURL is not picking up ANYTHING from it. It puts my page into a waiting loop then just quits and has no response back.

test.php Code:

<?php echo "works!"; ?>

Now the code that tries to get the "Works!" response.

<?php
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://SERVERIP/test/test.php"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch); 
curl_close($ch);
echo $output;
?>

Also, if it help, I recently just upgraded to a CDN Server. Contacted them and they said it's more than likely a software problem. So I figured I'd ask you all!

Any reply on help will be appreciated!

Answer

Pedro Lobito picture Pedro Lobito · Jan 14, 2014

Try using the CURLOPT_FOLLOWLOCATION

ex:

<?php
//I've added this two lines for debug, run the script and check for errors, when done, comment the lines.  
error_reporting(E_ALL); 
ini_set('display_errors', 1)

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://SERVERIP/test/test.php"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$output = curl_exec($ch); 
curl_close($ch);
echo $output;
?>

Based on your comments, I've updated the answer:

To Turn Off PHP Safe Mode on Your Linux Server

Using SSH, connect to your server.
Once you are logged in, type su - root.
For your password, type the same password you used to connect to your server.
At the command prompt type:
vi /etc/php.ini
Go to the line safe_mode = on and press the "i" key.
Change the line to safe_mode = off and press the "Esc" key.
Type :wq! to save your file.

To Turn Off PHP Safe Mode on Your Windows Server

Using Remote Desktop Connection, log in to your server as an administrator.  
Open c:\windowsphp.ini in Notepad.  
Change the line safe_mode = on to safe_mode = off.  
Save and close php.ini.  

Enabling curl on php.ini:

Windows:

Locate php.ini and uncomment:

extension=php_curl.dll

Linux:
On debian with apache2:

apt-get install php5-curl
/etc/init.d/apache2 restart

(php4-curl if it's php4)

Make sure your php was compiled with curl support, make a script with the following code:

<?php
echo phpinfo();
?>

search for CURL there.