-
Notifications
You must be signed in to change notification settings - Fork 633
Async PHP Requests Using CURL
CloudDueling.com edited this page Sep 7, 2013
·
1 revision
This waits for the CURL request to push to firebase but no one is waiting for it. ####application/controllers/firebase.php
<?php
class Firebase_Controller extends Base_Controller
{
public function post_index()
{
try {
$s = extend(array(
'url' => '',
'data' => array(),
), sr());
$fb = new firebase(Config::get('firebase.url'), Config::get('firebase.token'));
Log::fb($fb->push($s['url'], $s['data']));
return true;
} catch(Exception $e) {
return $e->getMessage();
}
}
}
// this makes a curl request to a url with the data // Note the " > /dev/null 2>&1 &" this tells the shell_exec to not wait for no one so it returns immediately. ####laravel/request.php
/**
* Make a CURL request.
*
* @return HttpFoundation\Request
*/
public static function curl($url, $payload, $type = "POST") {
$cmd = "curl -X {$type} -H 'Content-Type: application/json'";
$cmd.= "-v -d '" . json_encode($payload) . "' " . "'" . $url . "'";
$cmd .= " > /dev/null 2>&1 &";
return shell_exec($cmd);
}
// someone fills out a form and this makes the async request. ####application/controllers/comment.php
<?php
class Api_V1_Comment_Controller extends Base_Controller
{
public function post_index()
{
try {
$data = array(
'url' => "/products/{$conversation->serie_id}/comments",
'data' => eloquent_to_json($comment),
);
Request::curl('http://local.dev/firebase', $data);
} catch(Exception $e) {
return $e->getMessage();
}
}
}
Refer to step 3: https://segment.io/blog/how-to-make-async-requests-in-php/