How do I capture a 504 error in PHP cURL? - php

I have the CURL request below, and it is returning a 504 error. I want to echo out the fact that it is timing out to the UI, right now the UI isn't notified, but the Chrome console is. Is there a way I can grab the 504 and drop it in an if statement:
if ($error == 504) {
// tell the UI it timed out
} else {
// report the response
}
Right now I am using:
curl_getinfo($curl, CURLINFO_HTTP_CODE)
but it doesn't seem to grab the 504, probably because there was no response.
curl_setopt_array($curl, array(
CURLOPT_URL => $endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 60000,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_USERAGENT => $userAgent,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $payload,
CURLINFO_HEADER_OUT => true,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: Basic xxx'
),
));

curl_getinfo($curl, CURLINFO_HTTP_CODE)....but it doesn't seem to grab the 504
Barmar suggested that this question is a duplicate of PHP curl timeout error detection hpwever that describes a timeout implemented on the HTTP client - if you specifically want to get the timeout from the gateway/proxy you need to wait a bit longer (assuming it is online).
Most likely your default_socket_timeout is shorter than the timeout on the proxy. PHP is terminating the Curl call before it gets a response from the remote end. If unset, the default_socket_timeout is 60 seconds. Do beware that most browsers will timeout at 5 minutes after the initial HTTP request.

Related

cURL Error #:Operation timed out after 50007 milliseconds with 30461 out of 272389 bytes received

case 1
When run php file (http://localhost:8080/Rub_test.php) with below curl implemented code at postman then this error appear
cURL Error #:Operation timed out after 50007 milliseconds with 30461 out of 272389 bytes received
When increased CURLOPT_TIMEOUT => 500000 then error not appear but print_r($response); blank show.
case 2
When run only URL with authorization at postman then data show.
https://api.rubiproject.com/analytics/v1/report/?account=publisher%2F11990&start=2022-09-01T00%3A00%3A00-08%3A00&end=2022-09-02T23%3A59%3A59-08%3A00&dimensions=date%2Ccountry%2Csite&metrics=ad_requests%2Cauctions_won%2Cpaid_impression%2Crevenue
please suggest how to get data with curl-php. Or any mistake in curl.
please help me.
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.rubiproject.com/analytics/v1/report/?account=publisher%2F11990&start=2022-09-01T00%3A00%3A00-08%3A00&end=2022-09-02T23%3A59%3A59-08%3A00&dimensions=date%2Ccountry%2Csite&metrics=ad_requests%2Cauctions_won%2Cpaid_impression%2Crevenue",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 50,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic YjA2MzljMzJmYWM0OTA2ssssssssZWNmMjJiZjY0YjljZZWU4NzlkBMGVhMzkxODo0sswwNjk1OTc5YjRlYTaI1NWIyM2Y5NWFlZDE1M2YzODI0Yw==",
"cache-control: no-cache",
"content-type: application/json"
),
CURLOPT_FILE => $fp,
CURLOPT_FOLLOWLOCATION => true
));
$response = curl_exec($curl);
print_r($response);
$err = curl_error($curl);
Make curl timeout like this: CURLOPT_TIMEOUT => 0 to make it infinitely wait for results.
Also, you can read for time out info this
I've tried your code and it gives this error: Could not resolve host: api.rubiproject.com.
To see this error you can use :
if(curl_errno($curl)){
echo 'Curl error: ' . curl_error($curl);
}
for more info you can look at here
I suggest that you should check curl info by running this code to see your request's situation.
$info = curl_getinfo($curl, CURLINFO_HTTP_CODE);

Requesting a cURL GET Request in PHP outputs garbage

I'm trying to build a Connector to Dynamics 365 Business Central but I'm having problems getting the data. Please help me figure out why when I send a GET request using cURL and PHP, it produces the following output:
�S[O�0�+(��&��qB��i�t�F/���=�ا��Ďb�}N�"&�M�����������8�0% �;
Here is my code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.businesscentral.dynamics.com/{tenantID}/customers/?$filter=displayName%20eq%20'Shawn%20Test'",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: {Auth Code}"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
It seems like a character encoding issue, or maybe you are receiving binary data.
What is this endpoint supposed to return? Check the response headers, it might give you a clue.
This is an encoding problem, try to remove CURLOPT_ENCODING line from your request or try to choose different encoding from your browser

CRM show data using PHP CURL

I want to show my data on my CRM using PHP with curl and also using API.
If there is any other method. You can kindly show it to me
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://b.maxmind.ma/api/leads",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authtoken: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyfdfdiOjE1ODEwODk2MjJ9.Selc36cmT3XyXH6cGdJ3SN-332kx7"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
the response show : error code: 1020
I think the problem is that the website you are trying to request is protected by Cloudflare.
When i try by the postman , this is the header response a get :
The error code 1020 you got is from cloudflare .
First, you can try using this solution that bypasses the Cloudflare problem
KyranRana/cloudflare-bypass
If not working, in this case, you must contact the site owner to remove the block (more here)

PHP CuRL - Empty response due to SSL

I'm going to ask a question that's already been asked a lot, but I can't find a solution that works on my server.
I make a call with PHP on an API where I am correctly identified. I get the famous error 52 (Empty reply from server).
I tried all the parameters indicated on Stack but nothing works. I'm on a Ngnix server with an SSL Lets encrypt.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://xxx/route/rest?=",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 100,
CURLOPT_TIMEOUT => 300,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "app_key=" . env('APP_KEY')
));
$response = curl_exec($curl);
If you could help me, I would be very grateful.
Have a nice day
Eliott

Why does a custom curl HEAD request hang for weebly.com?

When I do
$h = get_headers('http://www.weebly.com');
It works just fine... The headers for that page are promptly returned.
But if I try to retrieve the headers via an explicit HEAD request using curl...
$url = 'http://www.weebly.com';
$request_headers = array(
'Connection: close',
);
$user_agent = 'curl/7.19.7 (x86_64-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15';
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_CUSTOMREQUEST => 'HEAD',
CURLOPT_HEADER => TRUE,
CURLOPT_HTTPHEADER => $request_headers,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_MAXREDIRS => 10,
CURLOPT_USERAGENT => $user_agent,
));
$result = curl_exec($ch);
The request does not finish.
What's wrong with my CURL setup? This works for other websites for http://www.google.com but for some like weebly it ends up hanging.
Because libcurl will act as if a GET is used but you change the method only in the actual request to HEAD, and that screw things up since the HEAD response probably has a Content-Length: header but no response body.
The "proper" way to have libcurl do a HEAD and expect a HEAD response is to use CURLOPT_NOBODY instead.
CURLOPT_CUSTOMREQUEST should only be used to change method keyword, not to change behavior.

Categories