I’m attempting to create a string $contacts_data that holds all of the data in the correct format required to be passed in my CURLOPT_POSTFIELDS property. However, my page is bombing with this current setup.
Here is the current output of my $contacts_data:
['email' => '[email protected]',],['email' => '[email protected]',],
However when I try to echo that data into my CURLOPT_POSTFIELDS, the site breaks:
<?php
$json = file_get_contents('current.json');
$contacts = json_decode($json, true);
$contacts_data = "";
for ($i = 0; $i < count($contacts); $i++) {
$contacts_data .= "[";
$contacts_data .= "'email' => '" . $contacts[$i]['Email'] . "',";
$contacts_data .= "],";
}
print_r($contacts_data);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://test.api-us1.com/api/3/import/bulk_import",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'callback' => [
'requestType' => 'POST',
'detailed_results' => 'true',
'url' => 'test.com'
],
'contacts' => [ echo $contacts_data; ]
]),
CURLOPT_HTTPHEADER => [
"Api-Token: 1234567890",
"Content-Type: application/json",
"accept: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}