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');
Related
My goal is to connect a private web service on my job.
Im trying to connect but when I set Content-Type to application/json it fails and return null, someone can help?
This is my code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json;charset=UTF-8')
);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
$fp = fopen(WAREHOUSE . '/errorlogc.txt', 'w');
curl_setopt($curl, CURLOPT_STDERR, $fp);
$json_response = curl_exec($curl);
when I execute this code it fails and return null and no more info, even the log I set for it show's me nothing.
fixed! the problem was that before trying to send data to the server I have to set a cookie for the session, so I get the cookie from the header response on the connection and then send the data.
thanks for the help
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"));
}
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 have to make a PATCH request using PhP cURL. I couldn't find any documentation, so I tried the following but it isn't working.
$data = "{'field_name': 'field_value'}";
$url = "http://webservice.url";
$headers = array('X-HTTP-Method-Override: PATCH');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
curl_close($curl);
Any idea why this isn't working? How can I fix it?
Edit:
I am connecting to a RESTful web service. It returns HTTP/1.1 200 for successful requests. Unsuccessful requests return HTTP/1.1 403. I keep getting 403.
I tried changing $data to:
$data = "data={'field_name': 'field_value'}";
It didn't change the outcome.
Edit2:
The final working code.
$data = "{'field_name': 'field_value'}";
$url = "http://webservice.url";
$headers = array('Content-Type: application/json');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
curl_close($curl);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH'); should do it.
JSON PATCH would be better for data format since this format is designed for HTTP PATCH request. See https://www.rfc-editor.org/rfc/rfc6902 for the spec. The tutorial of Rails 4 show the example(http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#http-patch).
// https://www.rfc-editor.org/rfc/rfc6902#section-4
$data = '{ "op": "add", "path": "/a/b/c", "value": "foo" }';
$headers = array('Content-Type: application/json-patch+json');
Try Using normal array
//$data = "{'field_name': 'field_value'}";
$data = array('field_name' => 'field_value' );
You can define methods and use everything in one curl function. Hope this helps you.
define('GI_HTTP_METHOD_GET', 'GET');
define('GI_HTTP_METHOD_POST', 'POST');
define('GI_HTTP_METHOD_PUT', 'PUT');
define('GI_HTTP_METHOD_PATCH', 'PATCH');
curl_setopt($ch, CURLOPT_POST, true);
if ($method === GI_HTTP_METHOD_PUT) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, GI_HTTP_METHOD_PUT);
}
if ($method === GI_HTTP_METHOD_PATCH) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, GI_HTTP_METHOD_PATCH);
}