I need to get the raw server response, with headers. This also means that gzipped or deflated content should still be compressed. I don't want any changes done to what is received.
Is this possible with PHP?
I tried with curl but that doesn't seem to be working, I set these to zero:
CURLOPT_HTTP_CONTENT_DECODING => 0,
CURLOPT_HTTP_TRANSFER_DECODING => 0,
But no help.
I tried with fsockopen but that seems to uncompress automatically as well.
Anything else?
Edit: these are all my curl headers:
$options = array(CURLOPT_URL => 'http://www.example.com/',
CURLOPT_CONNECTTIMEOUT => 20,
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_USERAGENT => $user_agent,
//CURLOPT_CUSTOMREQUEST => 'HEAD',
//CURLOPT_NOBODY => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//CURLOPT_HTTP_CONTENT_DECODING => 0,
//CURLOPT_HTTP_TRANSFER_DECODING => 0,
CURLOPT_BINARYTRANSFER => 1,
CURLOPT_HTTPHEADER => array('Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' => 'en-us',
'Accept-Encoding' => 'gzip, deflate'));
Thanks.
fsockopen won't automatically decompress, but you if you are rolling your own HTTP client, you must tell the server you're ready to accept a gzipped response, other the server will send you an uncompressed one.
You can do this by including an Accept-Encoding header in your request, e.g.
Accept-Encoding: compress, gzip
Related
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.
I'm getting 410 - unregistered failures. Even I am deleting my unregistered tokens. It was working fine for years and recently facing more number of issues.
I am passing over the bundle id properly.
if (!defined('CURL_HTTP_VERSION_2_0')) {
define('CURL_HTTP_VERSION_1_0', 3);
}
$url = "{$base_url}/3/device/{$appIosRegId}";
// headers
$headers = array(
"apns-topic: {$app_bundle_id}",
'Authorization: bearer ' . $jws
);
// other curl options
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
CURLOPT_URL => $url,
CURLOPT_PORT => 443,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $varData,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HEADER => 1,
CURLOPT_SSL_VERIFYPEER => false
));
I have not made changes related notifications and cross checked with all the programs. And everything set was proper only.
Does this needs any upgrade?
My current curl version is curl 7.83.1 (Windows) Release-Date: 2022-05-13
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
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.
I'm trying to make a post request to salesforce "api".
however it accepts only content type which is explicitly set to "application/x-www-form-urlencoded"
When I do this:
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://www.salesforce.com/servlet/servlet.WebToLead',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 3,
CURLOPT_POSTFIELDS => json_encode(array (
'first_name' => 'foo',
'last_name' => 'faa',
'email' => 'my.email#gmail.com',
'oid' => '#hash',
'recordType' => '#hash'
)),
CURLOPT_HTTPHEADER=>array(
'Content-type: application/x-www-form-urlencoded'
)
));
$data = curl_exec($ch);
$info = curl_getinfo($ch);
The response headers content-type is always:
"text/html;charset=UTF-8"
The same parameters I send using postman (with the correct header) actually works.
When CURLOPT_POST is true, curl sets Content-Type of the request to application/x-www-form-urlencoded automatically; you don't need to do that manually.
You can verify that (as well as check all other "outgoing" headers) by setting CURLINFO_HEADER_OUT to true prior to your request, then checking the array returned by curl_getinfo().
I suspect your problem has nothing to do with the request headers but with how curl handles https. Setting these in your curl_setopt_array() should help:
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,