Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Created September 14, 2013 14:11
Show Gist options
  • Select an option

  • Save clouddueling/6562352 to your computer and use it in GitHub Desktop.

Select an option

Save clouddueling/6562352 to your computer and use it in GitHub Desktop.
php async code laravel
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/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment