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.
Related
So I'm making a Moodle auth plugin for our internal system. For the part of the plugin that syncs updated profile information back to the external API, I need to make a PUT request.
The problem I'm experiencing is that a part of the request is sent through, but then it just stops and eventually the curl operation times out.
My code for making the request:
$curl = curl_init();
$headers = array();
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
array_push($headers, 'Content-Type: application/json');
array_push($headers, 'Expect:');
array_push($headers, 'Connection: keep-alive');
if ($authtoken){
array_push($headers, 'Authorization: Bearer '.$authtoken);
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, $serverurl.'/'.$functionname);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_FAILONERROR,true);
$result = curl_exec($curl);
if (curl_error($curl)){
curl_close($curl);
return false;
}
curl_close($curl);
return $result;
Most of this code works as intended, as it is used for other requests as well and works fine. Specifically for PUT requests like this one, though, it sends around 614 bytes out of the total, for example 614 out of 2324, according to Fiddler, then it stops. Eventually, it times out and I get an error.
So I've fixed it. The solution was to change this line:
curl_setopt($curl, CURLOPT_PUT, 1);
To this:
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
So, I have a php script which sends a large JSON object and this is the code I am using to do so.
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_VERBOSE, true);
$json_response = curl_exec($curl);
Now when the data is small, the request goes through smoothly. However, when the json object becomes huge it suddenly fails to send the data and instead is shows "Content-Length: 0" in the request header as if it did not include the object.
I am assuming there is a limit to how large the data could be. Is there a way to bypass that?
I have searched for similar issues and I found a solution for sending files. But the object i have here is a result set from the database and therefore I would prefer to keep the file approach as a last resort.
Thanks.
So basically you have the HTTPHEADER already but you're missing the Content-length one try it like so:
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-length:' . strlen($data)));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_VERBOSE, true);
$json_response = curl_exec($curl);
I need to integrate my website with right signature for signing documents.But I couldn't figured it out because its continously showing me Invalid OAuth Request.
I'm running this api by using php library but its keep on showing Invalid OAuth Request.I'm fed up from all this. Run on Browser below url
https://RightSignature.com/api/documents.xml
You're probably trying to access a file that needs you to have api accesses. If you haven't signed up with them, then you don't have access. If you just want to see the docs, here they are.
EDIT: btw, you need to request an API Key per their docs, and you can't even view their docs until you've signed up for an account. Once you do that, they will personally approve or deny you access to keys. With that, you have about one month to do something with it.
If you have secure token use this code:
For Json response:
Method defines GET or POST,
headers contains header part,
url is rightsignature endpoints,
secure token got after registration,
Body contains parameters.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Append 'api-token' to Headers
curl_setopt($curl, CURLOPT_HTTPHEADER, array($headers, "api-token: $secure_token")); // Set the headers.
// If you want parameters to be sent
if ($body) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
// Append 'api-token' to Headers
curl_setopt($curl, CURLOPT_HTTPHEADER, array($headers, "Content-Type: application/javascript;charset=utf-8", "api-token: $secure_token"));
}
For XML response:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Append 'api-token' to Headers
curl_setopt($curl, CURLOPT_HTTPHEADER, array($headers, "api-token: $secure_token")); // Set the headers.
// If you want parameters to be sent
if ($body) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
// Append 'api-token' to Headers
curl_setopt($curl, CURLOPT_HTTPHEADER, array($headers, "Content-Type: text/xml;charset=utf-8", "api-token: $secure_token"));
}
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 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.