I have a PHP-based REST API, is that possible to get multiple HTTP responses after I sent the request only once? or is there a way to achieve this goal?
for example, I send an HTTP request for updating a bunch of data, and I need the API response with the status after each process so I can show the % on the page, very similar to the loading bar.
currently, my php response header as below
http_response_code(200);
echo json_encode(
array(
"result" => "Request finished.",
"success" => true,
"progress" => $progress,
)
);
it only shows 100% once all actions are done, and I learned the once code 200 been sent to the client, the HTTP connection has been closed. I also tried return code 100, but not failed.
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 m trying to send a SMS from my trail Twilio account. so please help to get sms response/status
$client = new Client(TWILIO_SID, TWILIO_TOKEN);
$client->messages->create(
$mobile,
array(
'from' => TWILIO_FROM_NUMBER,
'body' => $mobile_message,
//'statusCallback' => "https://requestb.in/v9uqy6v9"
'statusCallback' => base_url()."sms_status.php"
)
);
//$status = file_get_contents('https://requestb.in/v9uqy6v9');
$status = file_get_contents(base_url()."sms_status.php");
when i run the above code i got error:
Message: file_get_contents(http://.../sms_status.php): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
while i manually browse the sms_status.php file url than it's not got any error
i also follow this link How to get notified when SMS Status changes from 'Queued' to 'Sent'?
so please help me to resolve above problem and also define what will be format of output like json/text/array.....
Twilio developer evangelist here.
When you set a statusCallback URL for an SMS message that you send, the URL needs to point to an application that can handle an incoming HTTP request.
When the status of the SMS message changes through queued, failed, sent, delivered, or undelivered Twilio will make an HTTP POST request to your URL, sending all the standard request parameters as well as two extra parameters; MessageStatus and ErrorCode. The parameters will be sent as URL encoded form parameters, so you should be able to read them using PHP's $_REQUEST[] syntax.
So, make sure your application can receive HTTP requests at the statusCallback URL and you can log the data out from there however you want to.
Let me know if that helps.
I'm trying to get some data from an endpoint. And the endpoint allows only requests that originate from one specific domain (which is not mine)
Is it possible to make a request with Guzzle and make it "pretend" as if it was coming from the allowed origin?
Currently I am trying to set some headers to achieve that and get the response back but it is always returning me code 200 with content-length: 0
You can set Origin header to what you want as long as you are making a request out of browser's control. In Guzzle it could be set like this:
$client->request('GET', '/data', [
'headers' => [
'Origin' => 'http://foo.bar',
]
]);
If targeting host is satisfied with this only header then you are fine otherwise you won't get your expected response.
I am trying to catch POST response send to me by external API.
The problem is that POST array is completely empty while I can check in firebug that browser recieved it but with codes 302 FOUND and second (with same body) with code 307 TEMPORARY REDIRECT:
Is there any way to grab this data inside my script or is this something wrong with server re-directions?
If you are using the CURL library, there are two options that help with your case:
curl_setopt($curl,CURLOPT_HEADER,1);
This returns the response header including the status code. You can see whether 302 is returned.
Or you can simply follow the redirect
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
Edit: sorry just saw you were doing this on the client side.
If this is an AJAX call, you can get the status code in the raw XHR object.
I am working on proxy with PHP. In my php code I am sending proper required headers and expected to get response body and headers. However I am getting response body correctly as I want but not getting headers properly (supposed to get Status 200 but getting 401). When i traced with firefox I found that SAP URL itsself making 2 request internally by using data which I send. so with my first request it is not authenticated so SAP url itslef managining to send same request again and 2nd time it gives both proper response body with headers. Howevber I php code when I get it i get response body from 2nd response and headers from 1st response.
here is code.
$opts = array(
'http'=>array(
'method'=>"POST",
'content' => $xml_request,
'header'=>array("Host:" . $sap_url,
"Content-Type: text/xml; charset=UTF-8",
$authstring,$xml_request)
)
);
$context = stream_context_create($opts);
$result = file_get_contents($sap_url, false, $context);
$http_res_array = get_headers($sap_url);
You should probably use curl functions instead and do BOTH requests yourself. file_get_contents, does the second request for you, but takes away the possibility to fetch the second headers.
Maybe a little old but anyways:
You're using the get_headers()-function to get the headers. It's documentation states that:
Fetches all the headers sent by the server in response to a [new] HTTP request
It doesn't empathize that this function will actually send a new request to the server and return the response-header for that request. Therefor, the headers can be slightly different.
Since you're using file_get_contents() to load the content, you can use the global $http_response_header-variable right after your request, which will contain the response-header from the last executed request.