2

I've searched high and low for the answer to this question but cant find anything...

We've got a single server and on it we've got a PHP service with an API.

We've recently written an PHP app which interacts with the API. When it goes live the API and the app will be on the same server.

However, when they are on the same server the cURL requests from the app to the API always return false. I'm sure this must be something to do with the way that the request is being routed by the server. Is there any way to make this work properly?

$url = 'http://api.some_address_on_the_same_server.com';
$postdata = array(...);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch); // $result is always false when on the same server for some reason
curl_close($ch);
2
  • are you using sessions ? Commented Jun 27, 2013 at 13:41
  • Hi Shankar, yes I am. Commented Jun 27, 2013 at 13:48

1 Answer 1

5

Must be related to locking session situations. Try this way.

<?php
$url = 'http://api.some_address_on_the_same_server.com';
$postdata = array(...);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

session_write_close();

$result = curl_exec($ch); // $result is always false when on the same server for some reason
curl_close($ch);

session_start();
?>

EDIT :

Have you added an exception in your windows host file ? /windows/system32/drivers/etc/hosts

like

127.0.0.1 yourdomain.com

For more information. Check out this

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but it still returns false.
The same issue. Specify domain in /etc/hosts and curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); helped me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.