为什么我的变量是空的?

时间:2015-04-29 15:43:35

标签: php

为什么$ book变量为空?我已经检查过它之前的一切,直到那一切都是正确的。我甚至尝试过重命名但是没有用:(

<?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>";
?>

我自己没有写,但execute_post是:

<?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;
}
?>

1 个答案:

答案 0 :(得分:0)

我改变了

curl_setopt($ch, CURLOPT_GETFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_GET, true);

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

它现在完美无缺!!