I am trying to do a PUT call on php using curlopt but the DELETE and POST methods work fine, and the PUT method no, I have an 400 error.
I am using the following code:
$url = 'https://theURL';
echo " \n URL:\n".$url." "; //The URL is correct
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$headers[] = 'Content-length: 0'; //I don't have a body so the header is 0
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
If i Don't put the header part, i have this error:
HTTP Error 411. The request must be chunked or have a content length.
But I don't have a body so I just put the content-length as 0. I have also tried to put a number there but I have a different error.
The error I am getting is this:
REQUEST ERROR: The server encountered an error processing the request.
Related
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_USERPWD, 'PortalPartner' . ":" . 'W169F1320&8d');
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header = curl_getinfo($ch);
curl_close($ch);
I need to see the response header where is will be token for my next request, but I can not.
When I use this request in insomnia or postman I can see response header but I can not in code.
You can specify a callback function for headers using curl_setopt with CURLOPT_HEADERFUNCTION. The callback will be execute for each header line.
$headers=[];
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use ($headers){
array_push($headers, $header);
});
Note that this will be executed for every header line including the initial HTTP/ line and will be provided with a simple string. You will need to split each line on ":" to separate the key and value. Typically, I would parse the headers into an associative array.
I am calling an API using CURL.
When I run it directly in browser or via ajax request it runs well and gives xml output.The Api am calling stores the xml in a database table and would only then work well.
However when I call it via PHP curl their table is not getting updated.
The code am doing it with PHP curl is
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Accept: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
Sample URL : http://example.com/code=x05a&businessKeyValue=8519ada0-9e2f-e265-8698-5b6145af9704&entity=funded_program_concession¶meters=fpc_funded_program_concession_id%7C8519ada0-9e2f-e265-8698-5b6145af9704¶meters=fps_funded_program_subsidy_id%7Cf320f2d9-7c6a-0b56-2940-5b61147a0f3d
If I open this link which opens it in browser, it works good, but if I run it via curl the API is not receiving xml content.
How can I resolve this issue?
$api='http://example.com/code=x05a&businessKeyValue=8519ada0-9e2f-e265-8698-5b6145af9704&entity=funded_program_concession¶meters=fpc_funded_program_concession_id%7C8519ada0-9e2f-e265-8698-5b6145af9704¶meters=fps_funded_program_subsidy_id%7Cf320f2d9-7c6a-0b56-2940-5b61147a0f3d';
$ch = curl_init();
$headers = array();//put your headers only if you need them
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result= curl_exec($ch);
return $result;
This should work for you. Inside your headers array make sure you put the headers you need and not extra. This is a simple curl, get call so i guess the above code will work without any trouble.
If you want to accept xml then you can got for Accept:application/xml or Accept:application/xhtml+xml
If the above methods don't work provide us with your error.
echo 'Curl error: ' . curl_error($ch);
Add the above line as a breaking point in your code and see what's the error.
My curl php code is returning 301 and i can't print out what the curl returned.
this is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://subdomain.thinkific.com/api/public/v1/users/3418346");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
$headers = array();
$headers[] = "X-Auth-Api-Key: myapikey";
$headers[] = "X-Auth-Subdomain: subdomain";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
var_dump($result);
Here is what it returned:
string(142) "301{"Location"=>"https:subdomain.thinkific.com/api/public/v1/users/3418346", "Cache-Control"=>"no-cache"}#"
i have tried json_decode the result to print it, still the same. as well as i also tried those:
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_NOBODY, 1); //this one returned string(0) without 301
I need to know if the error is from my server or it has something to do with my code. ( i did not share api key and subdomain for privacy sorry).
The 301 response code is the following :
The HTTP response status code 301 Moved Permanently is used for
permanent URL redirection, meaning current links or records using the
URL that the response is received for should be updated. The new URL
should be provided in the Location field included with the response.
SO it means that you're requesting a resource that has been moved (the url changed) and you should retry the request with the newly provided URL !
I'm trying to send the following CURL request in PHP. But its returning: "HTTP Error 411. The request must be chunked or have a content length."
PHP Script containing the CURL Request:
<?php
$numbers = 9999999999;
$message = "Test";
$message = urlencode($message);
$port = 80;
$url = "http://191.95.51.64/API/sendsms.aspx?loginID=myloginid&password=mypassword&mobile=".$numbers."&text=".$message."&senderid=ABCDEF&route_id=1&Unicode=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ( $ch, CURLOPT_PORT, $port );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ( $ch, CURLOPT_TIMEOUT, 20 );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
$output = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo $err;
} else {
echo $output;
}
?>
Output:
Length Required
HTTP Error 411. The request must be chunked or have a content length.
Since I'm new to use PHP Curl, so I couldn't be able to find out whats wrong. If anyone can briefly guide me the solution with an example, I'll be very appreciated to him. Thanks!
Since your data seems to be send as URL parameters, try adding content length header of zero like below:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
Also instead of touching the content length header, simply try to add an empty POST body. Based on which type of HTTP server you are posting to, the behaviour differs slightly (IIS, LightHTTPD, Apache).
Empty post body similar to:
curl_setopt($ch, CURLOPT_POSTFIELDS, array());
You forgot to close quote:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
I'm getting Error response as 405 Method Not Allowed while trying with PHP CURl Please find my below Code and help me I'm critical condition
<?php
$credentials = "xxxx:yyyyy";
$a=base64_encode($credentials);
$url = "http://api.trust.in/get/token";
$page = "/get/token";
$headers = array(
"POST ".$page." HTTP/1.1",
"Content-Type: text/plain;charset=utf-8",
"Content-Length:0",
"Authorization: Basic " . base64_encode($credentials),
"Connection:keep-Alive",
"Host:xyz.abc.in"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL,$url);
$data = curl_exec($ch);
print_r(get_headers($url));
?>
do not set you header content-length become zero, should give length of parameter that you post to site, to test it you can use postman. it easier to know what you doing wrong using that.
Based on the documentation you provided, the url used for the API endpoint is not correct.
Instead of:
http://api.trust.in/get/token
Use:
http://api.toyotautrust.in/get/token
Please try that and give us the results.
Added empty array $params because of zero parameters for POST Request according to my document
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));