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);
Related
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);
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);
I want to set a request header for a url xyz.com
is it the right way to set it in php?
header('Authorization: AuthSub token="xxxxxx"');
header('location:https://www.google.com/accounts/AuthSubRevokeToken');
I am trying to set the header for this URL for a call.But the Authorization: AuthSub header doesnt shows up in the request headers section of the FireFox NET panel.Which is used to show the requests.
Any idea about it?
Thanx.
I was using curl previously,But it didnt seemed to issue any request as i cant see it in the NET panel of FireFox.
Code is as follows:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"https://www.google.com/accounts/AuthSubRevokeToken");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: AuthSub token="1/xxx"'
));
$result = curl_exec($curl);
curl_close($curl);
echo 'hererer'.$result;exit;
header sets response headers, not request headers. (If you were trying to send a HTTP request elsewhere, it would have no effect.)
Please also note what the manual says about Remember that header() must be called before any actual output is sent, ....
And turn on error_reporting(E_ALL); before using header() to see if that is the issue for you.
Header names and values need to be separated by one colon plus a space, so the location "header" is just wrong, it should be:
header('Location: https://www.google.com/accounts/AuthSubRevokeToken');
(It's common to write the case this way, too, but not a need)
Next to that the header function is setting response headers, not request headers. So you're basically using the wrong tool.
In PHP you can not set request headers, that's part of the client (e.g. browser), not the server. So header just looks wrong here. Which HTTP client are you using?
A call, as in using CURL to request another page? The header() function applies only for web-browser<->server communications. It cannot affect any requests your server-side script does to other webservers. For that, you need to modify the particular method you're using, e.g. curl or streams.
For curl, see CURLOPT_HTTPHEADER here: http://php.net/curl_setopt
currently im using this code to get the headers:
get_headers( $url, 1 );
wondering how i can do with curl (more faster); this is my curl code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_exec($curl);
$info= curl_getinfo($curl);
curl_close($curl);
but i got "another" headers, i need the 'Location' header to see where go a bit.ly url or another redirect service.
Thanks in Advance.
If you want to extract the Location header, use this regex...
preg_match_all('/^Location: (?P<location>.*?)$/m', $headers, $matches);
var_dump($matches['location'][0]);
Ideone.
If you want to stop cURL from following the Location header, check out Charles' answer.
You want to turn the CURLOPT_FOLLOWLOCATION option is set to true.
edit:
Whoops, looks like you also need CURLOPT_RETURNTRANSFER to get the content back from curl_exec and CURLOPT_HEADER to make sure headers are included in that.
i have a hosted script somewhere that only accept POST request.
example, some.hosted/script.php
how can i setup another simple php that can accept GET request and then POST it to the hosted script.
so that i can put up a link like this: other.site/post2hostedscript.php?postthis=data
and then it POST postthis=data to the hosted script.
tnx
edit:
post2hostedscript.php do not give any result.
the result will go directly to some.hosted/script.php
just as if the user POST directly at the hosted script.
Your post2hostedscript.php will have to :
Fetch all parameters received as GET
Construct a POST query
Send it
And, probably, return the result of that POST request.
This can probably be done using curl, for instance ; something like this should get you started :
$queryString = $_SERVER['QUERY_STRING'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.othersite.com/post2hostedscript.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $queryString);
curl_exec($ch);
curl_close($ch);
For a list of options that can be used with curl, you can take a look at the page of curl_setopt.
Here, you'll have to use, at least :
CURLOPT_POST : as you want to send a POST request, and not a GET
CURLOPT_RETURNTRANSFER : depending on whether you want curl_exec to return the result of the request, or to just output it.
CURLOPT_POSTFIELDS : The data that will be posted -- i.e. what you have in the query string of your incoming request.
And note that the response from the POST request might include some interesting HTTP header -- if needed, you'll have to fetch them (see the CURLOPT_HEADER option), and re-send the interesting ones in your own response (see the header function).
Take a look at the "curl" functions, they provide everything you need.
You might consider replacing all instances of $_POST in the old script to $_REQUEST, which will result in it accepting both GET and POST alike.