get header information from php curl post request [duplicate] - php

This question already has answers here:
Show Curl POST Request Headers? Is there a way to do this?
(5 answers)
Closed 5 years ago.
I've been searching for hours and can't find anything on this. I'm doing a php curl post request to the sugarsync api and it returns a location in the headers that i need. i have no idea how to get this information. i have to keep it as a post because i post an xml file to their api and all they do is return header information. i have no clue how i can access the location in the headers. according to them i need to put it into another xml file and post that as well. any help is appreciated.

If you set the curl option CURLOPT_FOLLOWLOCATION, cURL will follow the location redirect for you.
If you want to get the headers, set the option CURLOPT_HEADER to 1, and the HTTP response you get back from curl_exec() will contain the headers. You can them parse them for the location.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1); // return HTTP headers with response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return the response rather than output it
$resp = curl_exec($ch);
list($headers, $response) = explode("\r\n\r\n", $resp, 2);
// $headers now has a string of the HTTP headers
// $response is the body of the HTTP response
$headers = explode("\n", $headers);
foreach($headers as $header) {
if (stripos($header, 'Location:') !== false) {
echo "The location header is: '$header'";
}
}
See all of the options at curl_setopt().

To get the header information in the response.
curlsetopt($ch,CURLOPT_HEADER,true);

Related

cUrl not returning response - response is BOOL

I am trying to verify my recapture with google, but I am getting a response of null
I copy and paste the information to Postman and sent the request and I received a positive response.
I copied the link in my browser as a GET request and I also got a response.
I am not sure what causing this, as all information is correct.
here is my code.
// set API URL
$url = 'https://www.google.com/recaptcha/api/siteverify';
// Collection object
$data = [
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //<--- my reCaptcha secret key
'response' => $_POST['recaptcha']
];
// Initializes a new cURL session
$curl = curl_init($url);
// Set the CURLOPT_RETURNTRANSFER option to true
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Set the CURLOPT_POST option to true for POST request
curl_setopt($curl, CURLOPT_POST, true);
// Set the request data as JSON using json_encode function
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
// Execute cURL request with all previous settings
$response = curl_exec($curl);
// Close cURL session
curl_close($curl);
echo 'the response was ' . $response . PHP_EOL;
I saw this but didn't help me. PHP cURL not return a response, POSTMAN returns response
Because it is an HTTPS url you may need to add:
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
It's a good idea to use CURLOPT_FOLLOWLOCATION.
I always use it. It is in my standard options.
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
While Google does not return a 302 redirect HTTP status code, they do something funny with the initial request. I made an HTML form and submitted it and a json response was returned but looking at the Browser's headers my initial request disappeared.
I do not think Google wants the post data as JSON.
In the API Request documentation is says METHOD:POST.
It says noting about making the request with JSON.
Google is expecting an array.
Try removing the json_encode().
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
If Google (unlikely) wants a JSON request, you need to add
Content-Type: application/json to the HTTP header.
By adding this header curl will put the "post data" in the body and will not use the
default POST header: application/x-www-form-urlencoded
$request = array();
$request = 'Content-Type: application/json';
curl_setopt($curl, CURLOPT_HTTPHEADER, $request);
I do not understand why there was no response. I would have at least expected:
{
"success": false,
"error-codes": [
"missing-input-secret"
]
}
You may want to add this code after your curl_exec($curl)
This should give you all the details of you request and Google's response.
$response = curl_exec($curl);
$info = rawurldecode(var_export(curl_getinfo($curl),true));
echo "<pre>\n$info<br>\n</pre>";
If you want to see your outgoing request header (recommended) add this option and the header will be in the curl_getinfo:
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
I'm a bit concerned about a NULL being returned. Was the word NULL returned? Or did you see nothing for $response in your string?
curl does not return a NULL. So maybe it was a false. Meaning there is likely a typo. echo does not show boolean false if $response was false you would not see it. Maybe add a
if($response == false){echo "curl failed<br>";}
Even if it failed, all the above is still true.
I looked over your code and I see nothing that would cause curl to fail. Even with the issues, there still should have been some sort of response. And there may have been an HTTP status code that would not show in your $response. It would be in the curl_getinfo
Are you using your local machine in linux? If that so, it is probably because you don't have curl installed in your system. So do
sudo apt install php{version}-curl

CURLOPT_HEADER returns 401

I need to get the location header. From what I've read, this should be as simple as
curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, false);
If I don't include those two options, the curl request works fine, but I'm not able to get the location header.
If I do include those two options, then I get a 401 error.
The Location that is being returned should be a URL that does require an additional login. What am I doing wrong?
If it makes a difference, I'm doing a PUT.
Update:
Turns out I was looking too much at the trees and not enough at the forest. When generated the information needed for a response to this question I realized the issue was actually with the call prior to this call.
Before the PUT, I need to get a session token. Since I wasn't parsing out the headers when getting the session token, I was getting a blank session token which was resulting in the 401 for the PUT.
HTTP response headers are included to the result of curl_exec when you set CURLOPT_HEADER option. So if you want to get response headers you have to extract it from the result using substr() and curl_getinfo(CURLINFO_HEADER_SIZE).
Here is a sample:
$c = curl_init();
// setting options including CURLOPT_HEADER
// ...
$result = curl_exec($c);
$info = curl_getinfo($c);
curl_close($c);
// extracting headers from result:
$n = $info['header_size'];
$headers = rtrim(substr($result, 0, $n));
$content = substr($result, $n);

get php virtual() response headers

The following makes a sub-request and outputs its body HTTP response content:
<?php
if (condition()) {
virtual('/vh/test.php');
}
?>
Is there a way to get its response headers?
My goal is to forward my request (with request headers) to other location on other host, which is accomplished with Apache ProxyPass directive, and set its response (headers and content) as the response to my request.
So my server would act as a reverse proxy. But it will test some condition which requires php context to be done, before forwarding the request.
Lets say, that current page has own original headers. By using virtual() you are forcing apache to perform sub-request, which generates additional virtual headers. You might get difference of those two header groups (by saving each with apache_response_headers()) by array_diff():
<?php
$original = apache_response_headers();
virtual('somepage.php');
$virtual = apache_response_headers();
$difference = array_diff($virtual, $original);
print_r($difference);
?>
However it will not help you to change current request headers because of this:
To run the sub-request, all buffers are terminated and flushed to the
browser, pending headers are sent too.
Which means, that you can not send headers anymore. You should consider usage of cURL instead:
<?php
header('Content-Type: text/plain; charset=utf-8');
$cUrl = curl_init();
curl_setopt($cUrl, CURLOPT_URL, "http://somewhere/somepage.php");
curl_setopt($cUrl, CURLOPT_HEADER, true);
curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($cUrl);
curl_close($cUrl);
print_r($response);
?>
You could use the HTTP Library - http://au1.php.net/manual/en/httprequest.send.php

How i can make this http post request with php json? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to send HTTP request and retrieve response in PHP (with fine-tuning of headers)?
8.1.4.1 Sample ping request
HTTP request:
POST /api/ra/v1/ping HTTP/1.0
Host: app.test.net
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
Content-Type: application/json
"Are you there?"
can someone please help me with some php raw example?
try curl
http://www.php.net/manual/en/curl.examples-basic.php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
I made the same thing for a game site, where I would have to constantly log in to use my turns. So instead of that I used curl to sign in, get authorization token and using it, send another request with curl to do certain actions. Then I ran that script with the windows task scheduler.
I have the code somewhere, I'll look into it when I get home if you still need help with this.

Curl doesnt return header information

i use curl to get the data (http request) from a website through php. and some of the information of the data is stored inside the header information. i can simply fetch the data using curl_exec, but if i try to fetch the header information using curl_getinfo, the information is missing. it supposed to be worked just like ajax. can somebody help me with this?
curl_getinfo doesn't give you the headers, it just gives meta information about the last request. The CURLOPT_HEADER options makes sure the headers are included in the output:
...
curl_setopt($c, CURLOPT_HEADER, true);
$data = curl_exec($c);
list($headers, $body) = explode("\n\n", $data, 2);

Categories