I know that when preforming POST Curl u set the content length using strlen($postdata)
However, when doing GET how can I determine the content length?
This is my code :
curl_setopt($CR, CURLOPT_URL, CLASS::SENDURL.$get);
curl_setopt($CR, CURLOPT_FAILONERROR, true);
curl_setopt($CR, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($CR, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($CR, CURLOPT_HTTPHEADER,
array('Content-Type: text/xml; charset=utf-8',
//'Content-Length: '.strlen($get),
'Content-Length: 0 ',
//
));
curl_setopt($CR, CURLINFO_HEADER_OUT, true);
Anyone knows how I can do that?
There is usually no entity body (casually called content) in a GET request. You do not appear to be sending any content and can probably omit the calls to set anything content related.
Related
The documentation says like this:
curl —X POST -c cookies.txt —d "login=demo&password=demo42" https://www.myadcash.com/console/login_proxy.php
Output will be:
{"token":"6333531373034343433623663646836383165693937383167373264323334663"}
My current code
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("login"=>"demo","password"=>"demo42"));
curl_setopt($ch,CURLOPT_URL,"https://www.myadcash.com/console/login_proxy.php");
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . 'cookies.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
No JSON response is showing. Although the text file is saving. Please help me to find right direction. Also if there is any error in the code, please let me know.
-d "login=demo&password=demo42" means data field, thats why pass as post fields not headers.
Therefore these two lines in your curl
curl_setopt($ch, CURLOPT_POST, 1); //Optional
curl_setopt($ch, CURLOPT_POSTFIELDS, "login=demo&password=demo42");
and you must get output.
On the basis of documentation, credentials must be post fields not header that's why no need to put login and password on header.
You don't need
curl_setopt($ch, CURLOPT_HTTPHEADER, array("login"=>"demo","password"=>"demo42"));
I'm hoping someone can help me. I'm sending an API request, via CURL, using PHP. The headers to the third party application need to be as follows:
$headers = array(
"content-type: application/json",
"Accept: application/json"
);
The CURL request is initialised and sent as follows:
// 1. initialize
$ch = curl_init();
// 2. set the options, including the url
curl_setopt($ch, CURLOPT_URL, $this->sendPropertyUrl);
curl_setopt($ch, CURLOPT_HEADER, $headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSLCERT, $this->certPath);
curl_setopt($ch, CURLOPT_CAINFO, $this->certPath);
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $this->certPassword);
curl_setopt($ch, CURLOPT_POSTFIELDS, $propertyDataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 3. execute and fetch the resulting HTML output
$rightmoveResponse = curl_exec($ch);
$output = json_decode($rightmoveResponse, true);
However if I trap the header info actually sent in the CURL request the output is as follows:
POST /v1/property/sendpropertydetails HTTP/1.1
Host: adfapi.adftest.rightmove.com
Accept: */*
Content-Length: 1351
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue
Can anyone explain why CURL has modified the Accept and Content-Type parameters?
Any help greatly appreciated.
Brian
What you want is defined by CURLOPT_HTTPHEADER instead of CURLOPT_HEADER.
From PHP Docs:
CURLOPT_HEADER TRUE to include the header in the output.
CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')
See this user note about using it.
You used curl_setopt($ch, CURLOPT_POST, true); which sets the content-type. Instead use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); (see http://php.net/manual/en/function.curl-setopt.php).
This is in addition to what #Alan Machado said.
Below is an example without CURLOPT_HTTPHEADER, and it works fine.
<?php
$URL = "http://someurl.info";
$data = 'x_test_request=0&x_version=3.1&x_delim_data=true&x_relay_response=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 1800);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
?>
Does Curl auto-detect the type of data we Post, and auto-count the string length?
Or should we always specify Both?
Or does it largely depend on the destination's rules and expectations?
To be safe, I usually add the header info like this:
<?php
$contentlen = strlen(trim($data));
$headersARR = array(
"Content-type: application/x-www-form-urlencoded",
"Content-length: ".$contentlen,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headersARR);
?>
I read http://us1.php.net/curl_setopt, but haven't noticed any details on defaulted content-type parameters.
If you do not specify anything about Content-type but use the CURLOPT_POSTFIELDS then the header is always Content-type: application/x-www-form-urlencoded as default. Also it sets the Content-length: automatically.
For this reason, when we post JSON content through the POST we need to override the content-type through HTTPHEADER option. It is similar to XML or any other specific type of content.
I have a cURL request that is giving me a 411 length required error if execute without the Content-Length header. When I add $header[] = 'Content-Length: 0 to my array of headers, it is executed slowly (~15sec.).
I use the same code on a different domain without any problem. On this domain, I don't need to set the content-length header and I don't receive a 411 error. I'm thinking the Content-Length error might be related to the issue since only a PUT request should give me this error.
This is my current cURL request:
$curl = curl_init();
$header[] = 'Authorization: Basic 123123';
$header[] = 'Accept: text/xml';
//$header[] = 'Content-Length: 0';
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curl);
$error = curl_error($curl);
Edit:
I've changed my CURLOPT_URL and added CURLOPT_POSTFIELDS.
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
instead of
curl_setopt($curl, CURLOPT_URL, $url . $data);
This way I was able to eliminate the need for a Content-Length header but the request is still terribly slow.
Some servers require you to set the content length, others don't. This is mainly used so it can base it's buffer on that.
You shouldn't add 0 as content length, but the amount of data that will be sent. If you set it as 0, some servers will refuse to load it.
What I'm guessing is happening is because you set it to 0, it'll read the content byte / byte which is obviously very slow.
I try to make a post request with php and curl. Here is my code
//PHP 5.3.5 and curl: 7.18.2
$ch = curl_init();
if(!empty($save_cookie)){
curl_setopt($ch, CURLOPT_COOKIEJAR, $save_cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $save_cookie);
}else{
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, 'http://localhost/post.php');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pars);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_NOBODY, !$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
if (curl_errno($ch)) {
return false;
}
curl_close($ch);
return $postResult;
In http://localhost/post.php, I write
print_r($_SERVER);
The result return of curl is always
[REQUEST_METHOD] => GET
Remove the CURLOPT_NOBODY option and it will work. Or place it above the CURLOPT_POST line.
I think I have encountered this once, when trying to get just the header of a response. Setting
curl_setopt($ch, CURLOPT_NOBODY, true);
effectively instructs curl to issue a HEAD request, which is not a POST request. I think there is no way to just get the header from a POST (and just drop the connection after receiving the header). As a side effect, setting CURLOPT_NOBODY to false sets the request type to GET...
Do you really need the CURLOPT_NOBODY flag?
Try to move the
curl_setopt($ch, CURLOPT_NOBODY, !$body);
line right before the
curl_setopt($ch, CURLOPT_POSTFIELDS, $pars);
line.
There's an interesting post at the curl/set_opt page, shedding some light on this behaviour:
If your POST data seems to be disappearing (POST data empty, request
is being handled by the server as a GET), try rearranging the order of
CURLOPT_POSTFIELDS setting with CURLOPT_NOBODY. CURLOPT_POSTFIELDS has
to come AFTER CURLOPT_NOBODY setting because if it comes after it
wipes out the header that tells your URL target that the
request is a POST not a GET.