I am trying to fetch geocodes from an api and update in my database against address entries. I am running this through a seed class in laravel.
And I am using Guzzle to make an asynchronous call. I want to run the api calls asynchronously while at the same time I want to read the asynchronous response in the background and update them in the database.
$client = new \GuzzleHttp\Client();
//retrieve the the latitude and longitude from geocode.farm of the given address
$response = $client->get('http:........<url for json request goes here>',['future' => true]);
$response->then(function ($response) {
// in here I read the $response object and get the latitude /longitude to update in the database.
// I tried to echo and print here, it seems the script is not entering here in this callback function
});
I am calling the above line of code in a loop. The above script runs fine when I make a synchronous call, but in asynchronous call I am unable to run it? can you please help me with it.
the script doesn't seem to enter in the callback function
Related
I create the code below to make an async request to get data from an API.
<?php
require 'vendor/autoload.php';
$url = "https://pucminas.instructure.com/api/v1/";
$client = new GuzzleHttp\Client(['base_uri' => $url]);
$headers = [
'Authorization' => "Bearer 11111~dsgffdhstggfjsdhf",
'Accept' => 'application/json',
];
$course_id = "2242";
set_time_limit(300);
$promise = $client->getAsync( 'courses/'.$course_id.'/students/submissions?student_ids[]=all&grouped=true&post_to_sis=false&enrollment_state=active&include[]=user&include[]=assignment&include[]=total_scores&per_page=1000', ['connect_timeout' => 600, 'headers' => $headers]);
$promise
->then(function ($response) {
echo 'Got a response! ' . $response->getStatusCode();
});
?>
<h2>Reports</h2>
I suppose when I load the page it will show the text "Reports" in the page and after getting the content from the API(which needs at least 60 seconds), it will show the message "Got a response", but it won't work that way.
First, the page loads the content from the API and only after that show the text "Reports" and "Got a message" at the same time.
I want the text "Reports" appear instantly when I load the page, and the text "Got a response" only when the data is loaded from the API.
How can I do that?
You can only send a single response from a PHP script i.e. your browser requests a page, the PHP server builds that page and sends it back. The PHP server can't then 'push' an update of the page to your browser as HTTP is stateless.
To achieve the functionality you want you need to write some javascript which will call a PHP script to start the guzzle async promise. This promise would then write the result to a file (or database) when the promise is complete.
You then need a second javascript function which continually checks a second php script. The second PHP script would check the contents of the file (or database entry etc) and return the result.
You might be better to look at something like Axios and lose the PHP altogether
https://www.npmjs.com/package/axios
I am trying to get page meta tags and description from given url .
I have url array that I have to loop through to send curl get request and get each page meta, this takes a lot of time to process .
Is there any way to process all urls simultaneuosly at same time?
I mean send request to all urls at same time and then receive
response as soon as request is completed respectively.
For this purpose I have used
curl_multi_init()
but its not working as expected. I have used this example
Simultaneuos HTTP requests in PHP with cURL
I have also used GuzzleHttp example
Concurrent HTTP requests without opening too many connections
my code
$urlData = [
'http://youtube.com',
'http://dailymotion.com',
'http://php.net'
];
foreach ($urlData as $url) {
$promises[] = $this->client->requestAsync('GET', $url);
}
Promise\all($promises)->then(function (array $responses) {
foreach ($responses as $response) {
$htmlData = $response->getBody();
dump($profile);
}
})->wait();
But I got this error
Call to undefined function GuzzleHttp\Promise\Promise\all()
I am using Guzzle 6 and Promises 1.3
I need a solution whether it is in curl or in guzzle to send simultaneous request to save time .
Check your use statements. You probably have a mistake there, because correct name is GuzzleHttp\Promise\all(). Maybe you forgot use GuzzleHttp\Promise as Promise.
Otherwise the code is correct and should work. Also check that you have cURL extension enabled in PHP, so Guzzle will use it as the backend. It's probably there already, but worth to check ;)
I'm running a websocket server made with php (Ratchet http://socketo.me/) that is working fine when i connect with javascript native API. But when I try to make a push to the server using php it just take too long (10 seconds or more!).
Ratchet does not provide (as far as I know) any method to make a push with its native API, so im using this one: https://github.com/Textalk/websocket-php
The request is very simple:
//...
//Insert data into database ...
//Push data to the server, so the other clients get updated
$data = json_encode($requestData);
$client = new \WebSocket\Client("ws://localhost:10000");
$client->send($data); //<--10 sec request :(
//...
I took a look in the client code and added some lines (for testing purposes only):
stream_set_blocking($this->socket, 0);
and
$client->setTimeout(1);
But doesn't seems to work. Any help will be very appreciated.
i am beginner to graph api/php . i want to get events data against page_id etc.when i paste url into browser it returns json data but when i use curl or file_get_contents(); method it returns "Null". What is the proper way to sending request and getting response. this is my basic code and other than this i am NOT using any kind of include files etc.
$json_link = "https://graph.facebook.com/getwellgabby/events/attending/?fields=id,name,description,timezone,start_time,cover&access_token=206516302813755|lRnrKAiu3Z......&since=1356998400&until=1483228800";
file_get_contents($json_link);
$obj = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
var_dump($json);
RESULT: boolean (false)
https://developers.facebook.com/docs/graph-api/reference/v2.3/event/attending
As you can see in the docs, the correct way to get attendees for an event is to use the Event ID:
https://graph.facebook.com/{event-id}/attending?access_token=xxx
You canĀ“t get all attendees for all events in one API call, you have to do this event by event. Test it in the API explorer before using any PHP code.
Btw, you should use CURL instead of file_get_contents. The PHP SDK uses CURL too.
I'm using Guzzle that I installed via composer and failing to do something relatively straightforward.
I might be misunderstanding the documentation but essentially what I'm wanting to do is run a POST request to a server and continue executing code without waiting for a response. Here's what I have :
$client = new \GuzzleHttp\Client(/*baseUrl, and auth credentials here*/);
$client->post('runtime/process-instances', [
'future'=>true,
'json'=> $data // is an array
]);
die("I'm done with the call");
Now lets say the runtime/process-instances runs for about 5mn, I will not get the die message before those 5mn are up... When instead I want it right after the message is sent to the server.
Now I don't have access to the server so I can't have the server respond before running the execution. I just need to ignore the response.
Any help is appreciated.
Things I've tried:
$client->post(/*blabla*/)->then(function ($response) {});
It is not possible in Guzzle to send a request and immediately exit. Asynchronous requests require that you wait for them to complete. If you do not, the request will not get sent.
Also note that you are using post instead of postAsync, the former is a synchronous (blocking) request. To asynchronously send a post request, use the latter. In your code example, by changing post to postAsync the process will exit before the request is complete, but the target will not receive that request.
Have you tried setting a low timeout?