php - Why is my variable empty? -
why $book variable empty? i've checked before , until point correct. i've tried renaming didn't work :(
<?php $postcontacturl = 'https://apiconnector.com/v2/contacts/'; $data = array( 'email' => $_get['email'], 'emailtype' => 'html', 'datafields' => array( array( 'key' => 'fullname', 'value' => $_get['name_first']." ".$_get['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>"; ?>
i didn't write myself execute_post is:
<?php //function initiate curl, set curl options, execute , return response function execute_post($url, $data){ //encode data 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 , json decode response $responsebody = json_decode(curl_exec($ch)); //close curl session curl_close($ch); return $responsebody; } ?>
i changed
curl_setopt($ch, curlopt_getfields, $requestbody); curl_setopt($ch, curlopt_get, true);
to
curl_setopt($ch, curlopt_customrequest, "post"); curl_setopt($ch, curlopt_postfields, $requestbody);
and works perfectly!!
Comments
Post a Comment