PHP cURL Content-Length - php

I am trying to add a record to the database with POST operation via API and I generated the attached code from Postman.
On Postman, the process goes without any problem and user registration takes place.
But when I try to generate the code as "PHP cURL" via Postman and run it on PHP, I get the relevant error.
HTTP Error 411. The request must be chunked or have a content length.
I defined POSTFIELDS as array, calculated Content-Length using strlen function and added it to Headers. But it didn't work.
My PHP code;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://xxx.xxx/xxx/xxx/xxx',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"TokenKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"EmployeeList": [
{
"Example1": "1",
"Example2": "somedata2",
"Example3": "somedata3",
"Example4": "somedata4",
"Example5": "somedata5",
"Example6": "somedata6",
"Example7": "somedata7",
"Example8": "somedata8",
"Example9": "somedata9",
"Example10": "somedata10",
"Example11": "somedata11",
"Example12": "somedata12",
"Example13": "somedata13",
"Example14": "somedata14",
"Example15": "somedata15",
"Example16": "somedata16",
"Example17": "somedata17",
"Example18": "somedata18",
"Example19": "somedata19",
"Example20": "somedata20",
"Example21": "somedata21",
"Example22": "somedata22",
"Example23": "somedata23",
"Example24": "somedata24",
"Example25": "somedata25",
"Example26": "somedata26",
"Example27": "somedata27",
"Example28": "somedata28",
"Example29": "somedata29",
"Example30": "somedata30",
"Example31": "somedata31",
"Example32": null,
"Example33": null,
"Example34": null,
"Example35": "2022-12-06 22:05:22",
"Example36": false,
"Example37": true
}
]
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
echo "<br>";
echo $statusCode;````

Related

Amazon SP API - createFeedDocument

The goal is to do a "POST_FLAT_FILE_PRICEANDQUANTITYONLY_UPDATE_DATA" so right now I'm trying to do a createFeedDocument which I believe is the 1st step.
There's few questions about this but none in PHP curl.
I spent hours....HOURS and still can't figure it out..
I get error:
"message": null,
"code": "InvalidInput"
I even tried removing "contentType" from the URL and put it in the header, but then I get error msg: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method."
Please....please is there anyone out there that knows what I need to change with code below?
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://sellingpartnerapi-na.amazon.com/feeds/2021-06-30/documents?contentType=text/tab-separated-values;charset=UTF-8',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'x-amz-access-token: the token<hidden>',
'X-Amz-Date: 20230125T073105Z',
'Authorization: AWS4-HMAC-SHA256 Credential=The_AccessKey/20230125/us-east-1/execute-api/aws4_request, SignedHeaders=accept;host;x-amz-access-token;x-amz-date, Signature=a6e8bdf306ea24438978a5fff961d08fc65d06c10f43c782af15a6a60<hidden>'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

Trouble with Param arguments for an Api

Im trying to get all cities of a specific country from an API(https://countriesnow.space/). Im using Php and curl but the result says:
{
"error": true,
"msg": "missing param (country)"
}
Im using a script which is given by but it does not work: https://documenter.getpostman.com/view/1134062/T1LJjU52#4829d16f-0f4e-43ec-886e-68ebad1221d8
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://countriesnow.space/api/v0.1/countries/cities',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"country": "nigeria"
}',
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Could anyone tell me whats wrong here? Really cant tell whats the issue.
cURL will automatically set the Content-Type to application/x-www-form-urlencoded, when you pass a string value for CURLOPT_POSTFIELDS.
But this is not supposed to be form data, but JSON. Add
CURLOPT_HTTPHEADER => ['Content-Type: application/json']
to your cURL options, to let the API know you are in fact sending JSON.

WordPress JSON API AUTH with PHP Curl

I am working with wordpress and using this plugin https://wordpress.org/plugins/json-api-auth/ for JSON API Auth and I am trying to return success with curl in PHP but whenever I run the code I am getting {"status":"error","error":"Invalid username and\/or password."}
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="abcd"&password="abcd"',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
But when I put this in the browser url https://example.com/api/auth/generate_auth_cookie/?username=abcd&password=abcd I am getting success
{"status":"ok","cookie":"xyz","cookie_name":"xyzxyz","user":{"id":1,"username":"abcd","nicename":"abcd","email":"abcd#abcd.com","url":"","registered":"2020-09-10 10:42:41","displayname":"abcd","firstname":"abcd","lastname":"abcd","nickname":"abcd","description":"","capabilities":{"administrator":true},"avatar":null}}
Can you help me what I am doing wrong with curl in PHP?
Original version of CURLOPT_URL that I have used in code
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="'.$this->input->post('email').'"&password="'.$this->input->post('password').'"',
Remove the " from the cURL url query string. The WordPress PHP handling your request would read your username as "abcd" instead of abcd as you are trying to achieve.
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="abcd"&password="abcd"'
Should be
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username=abcd&password=abcd'

cURL: wait for response until it is processed by api

everybody,
I am using the following code for synchronizing data with an API ($method= POST) and then I call using the GET method ($method= GET) to print the result of the synchronization:
function sendCurl ($fileName, $method)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => array('file' => new CURLFILE($fileName)),
CURLOPT_HTTPHEADER => array(
"Authorization: xxxxxxxxxxxxxxx",
"Accept: application/json."
"Content-Type: multipart/form-data"
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
The problem is that the GET method response arrives before the API has time to process it. Therefore, when I print the response I print WAITING FOR RESPONSE and not whether it went ok or failed.
What can I do to print the ok or fail and not the WAITING FOR RESPONSE?
Thank you very much!

AtTask API 'PUT' CURL Fails to Update

I have been working on some code to automate an update to a specific task in AtTask using the API. I am finding some problems using PHP's CURL request for a PUT command. Here is a copy of the code:
$updateURL = "https://[COMPANYURL].attask-ondemand.com/attask/api/v4.0/task?updates={'ID':'TASKID','name':'Ok. Here we go again','commitDate':'2015-10-27T17:30'}";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $updateURL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_HTTPHEADER => array(
"sessionid: " . $this->sessionId
),
));
$attask_update_json = curl_exec($curl);
The code executes with out any errors being reported, but the return response is blank. I have verified my URL using postman and it processes correctly, but I can not get the code to process in PHP. Any help would be appreciated.

Categories