I make a PHP CURL PUT JSON request with basic AUTH via API:
$data = <<<JSON
{
"name": "John",
"age": "32",
"address": {
"city": "London",
"street": "Laxin street 32",
"zip": "32567"
}
} JSON;
Here is what I have:
$data_json = json_encode($data);
$url = "https://www.website.com/record?apiKey={API_KEY}";
$username = "user";
$password = "123abcd";
$encodedAuth = base64_encode($username.":".$password);
$curl = curl_init();
curl_setopt_array($curl, [CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array("Authentication : Basic ".$encodedAuth),
CURLOPT_USERPWD => $username.":".$password,
CURLOPT_HTTPAUTH => CURLAUTH_ANY,
CURLOPT_POSTFIELDS => $data_json,
CURLOPT_HTTPHEADER => [
"Cache-Control: no-cache",
"Content-Type: application/json",
"TOKEN: SAMPLE_TOKEN"],]);
$response = curl_exec($curl);
curl_close($curl);
In response, I got the original record data without changes that I sent via API.
How can I request that PUT using PHP's curl with basic auth? I tried some answers to similar questions but with no success.
CURLOPT_HTTPHEADER? one is overwriting the other. Also why JSON like you posted and why not a php array and thenjson_encodeit, there must be a problem with they way you are defining the JSON. Also if you are setting authentication header then whyCURLOPT_USERPWDandCURLOPT_HTTPAUTH?