Why is my variable empty?

Why is the $book variable empty? I’ve checked everything before it and up until that point everything is correct. I’ve even tried renaming it but that didn’t work :worried:

<?php
    $postContactUrl = 'https://apiconnector.com/v2/contacts/';
    $data = array(
    'Email' => $_POST['email'],
    'EmailType' => 'Html',
    'dataFields' => array(
    array(
    'Key' => 'FULLNAME',
    'Value' => $_POST['name_first']." ".$_POST['name_last'] ),
    )
    );
    $contact = execute_post($postContactUrl, $data);
    
    $addContactToAddressBookUrl = 'https://apiconnector.com/v2/address-books/' . '123456' . '/contacts';
    $book =  execute_post($addContactToAddressBookUrl, $contact);
    
 echo "<pre>" . print_r($book, true) . "</pre>";
    ?>

When you add the ‘true’ second parameter, the print_r function returns the value rather than prints it. Is this what is causing the confusion?

The fixes are likely to be either

print_r($book);  // without the true

Or

echo print_r($book, true); // kinda pointless but will likely produce the output desired

Does that help?

I guess because execute_post() function returns nothing. Check that function code.

If I call the whole thing in a separate file using AJAX to submit the form it works but now it’s not. I understand what you’re saying about the execute_post function is empty but I don’t understand why or how to fix it?

<?php
//Function to initiate curl, set curl options, execute and return the response
function execute_post($url, $data){
//encode the data as json string
$requestBody = json_encode($data, true);

//initialise curl session
$ch = curl_init();

//curl options
curl_setopt($ch, CURLAUTH_BASIC, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, 'username' . ':' . 'password');
curl_setopt($ch, CURLOPT_GETFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_GET, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . 'application/json' ,'Content-Type: application/json'));

//curl execute and json decode the response
$responseBody = json_decode(curl_exec($ch));

//close curl session
curl_close($ch);

return $responseBody;
}
?>

I did try

echo print_r($book, true);

but that just outputted Array ( ) 1 which still didn’t add the data I’m afraid

Ah yes, @megazoid was right about the function not returning anything (well, an empty array in this case)

I would guess your problem is that you’re trying to send JSON on the query string (CURLOPT_GETFIELDS) rather than as the request body.

I found this article, which might fix things for you - the json should be sent in the body rather than on the url

Thanks, I’ll have a read of that and report back.

Sorry to be dumb but I don’t understand why this worked using AJAX but not in the same file?

Thank you so much, I read that article and change the (CURLOPT_GETFIELDS) and (CURLOPT_GET) to

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);

and it now works perfectly!! :smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.