У меня есть скрипт PHP для обновления контактной информации через CURL, он должен сделать 2 обновления, одно для клиентов, 1 для контактов. Клиент работает, но контакты просто обновляют базу данных пробелами, она не оставляет значений, она полностью стирает их, кто-нибудь может увидеть причину этого?
Спасибо
Код ниже: itemid и contactid объявлены в скрипте и работают нормально
//Update Client
$name = "test2 test2";
$address1 = "my house";
$city = "my city";
$state = "my state";
$postal_code = "NE25 8RU";
$work_phone = "01234567";
$first_name = "test2";
$last_name = "test2";
$email = "test@test.com";
$url = urlencode("https://myurl.co.uk/api/v1/clients/$itemid");
$encoded_url = urlencode($url);
$after_encoded_url = str_replace("%2F", "/", $url);
$after_encoded_url2 = str_replace("%3A", ":", $after_encoded_url);
$after_encoded_url3 = str_replace("%3Cbr+/%3E", "/", $after_encoded_url2);
$curl = curl_init();
if (!$curl) {
die("Couldn't initialize a cURL handle");
}
$headers = [
'Content-Type: application/json',
'X-Requested-With: XMLHttpRequest',
'X-Ninja-Token:token',
];
$cid = (int)$contactid;
$postdata3 = array(
"name" => $name,
"address1" => $address1,
"city" => $city,
"state" => $state,
"postal_code" => $postal_code,
"work_phone" => $work_phone,
"contacts" => array(array(
"id" => $cid,
"client_id" => $itemid,
"first_name" => $first_name,
"last_name" => $last_name,
"email" => $email,
"phone" => $work_phone
))
);
$postdata = json_encode($postdata3);
echo $after_encoded_url3;
// Set the file URL to fetch through cURL
curl_setopt($curl, CURLOPT_URL, $after_encoded_url3);
// Set a different user agent string (Googlebot)
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
// Follow redirects, if any
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// Fail the cURL request if response code = 400 (like 404 errors)
curl_setopt($curl, CURLOPT_FAILONERROR, true);
// Return the actual result of the curl result instead of success code
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Wait for 10 seconds to connect, set 0 to wait indefinitely
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
// Execute the cURL request for a maximum of 50 seconds
curl_setopt($curl, CURLOPT_TIMEOUT, 50);
// Execute the cURL request for a maximum of 50 seconds
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
// Execute the cURL request for a maximum of 50 seconds
curl_setopt($curl, CURLOPT_POST, "PUT === 'POST' ? 1 : 0");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata3);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
// Do not check the SSL certificates
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// Fetch the URL and save the content in $html variable
$html = curl_exec($curl);
// Check if any error has occurred
if (curl_errno($curl))
{
echo 'cURL error: ' . curl_error($curl);
}
else
{
// cURL executed successfully
return $html;
}
// close cURL resource to free up system resources
curl_close($curl);