1

I am trying to execute the background REST API Call with Curl library in php. I guess it is not working.

can you suggest me ?

$cum_url       = http://localhost/test/list;
$post = [ 'id' => $object->id ];
$ch = curl_init($cum_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);         
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
curl_exec($ch);
curl_close($ch);

UPDATE: Cull error says "Timeout was reached".

Thanks, Raja K

3
  • reference : paul-norman.co.uk/2009/06/asynchronous-curl-requests Commented Mar 3, 2016 at 7:31
  • Please include your error or other findings in the question. How do you know it's not working? I hope you didn't really guess, but tested it first ;) Commented Mar 3, 2016 at 7:47
  • Hi, Thanks. I have added a db insert line in the REST URL. It's not pinging. It's not working after I have added curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1); for background process Commented Mar 3, 2016 at 7:58

1 Answer 1

0

You need to transfer your post data from array to http parameter string, ex:

$cum_url = "http://localhost/test/list";
$post = [ 'id' => $object->id ];
$postdata = http_build_query($post);

$options = array (CURLOPT_RETURNTRANSFER => true, // return web page
    CURLOPT_HEADER => false, // don't return headers
    CURLOPT_FOLLOWLOCATION => true, // follow redirects
    CURLOPT_AUTOREFERER => true,
    CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1",
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_SSL_VERIFYPEER => false);
$ch = curl_init($cum_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt_array ( $ch, $options );
$res = null;
if(!curl_errno($ch)) {
    $res = curl_exec($ch);
}
curl_close($ch);

Of course, some option is optional depends on you, ex CURLOPT_USERAGENT. Just show you an example.

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

4 Comments

Thanks. Normal CURL is working. I am talking about background process. Are you saying that you're snippet support background process ?
Please refer to php.net/manual/en/function.curl-setopt.php to make sure you understand the curl options and only use the neccessary options and not just copy options from other examples ... Also CURLOPT_RETURNTRANSFER when set to true does not return web page, but returns the response as a string instead of outputting it directly ..
Sorry, I misunderstand your question. If you are looking for send asynchronous call with curl, maybe you can try this. stackoverflow.com/questions/26039848/…
DTH, thanks for your remind. Yes, you are right, always better to check the using options from document before using it.

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.