将JSON数据POST到API脚本而不解析数据

时间:2014-12-30 01:35:31

标签: php json curl

我试图使用cURL发布json数据 一个API脚本,用于将数据提交给 应用。我使用file_get_contents(' php:// input') 并且数据未提交给应用程序 但是,如果我在" contact_email"中键入实际的电子邮件地址。 在API中,它将电子邮件提交给应用程序 首先是cURL脚本:

`

$data = '
 {

 "customer":
     {
      "first_name":"John",
       "last_name":"Smith",
       "email":"frednow9901@aol.com",
       "phone_number":"2125555555",
       "billing_address":"212 Any Street",
       "billing_city":"Any City",
       "billing_state":"New York",
       "billing_zip":"10012",
       "billing_country":"USA"
      }


}

';                                                                                                                                                     

$ch = curl_init('http://example.com/acadd.php');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

`

,这是它发布到的API脚本:

 <?php


$json_data = file_get_contents('php://input');

$cart = json_decode( $json_data );

$email = $cart->customer->email . 

// Set up an object instance using our PHP API wrapper.
define("AC_URL", "https://account.api-us1.com");
define("AC_API_KEY", "api key");
require_once("./ac-api-php/includes/ac.class.php");
$ac = new AC(AC_URL, AC_API_KEY);

$post_data = array(
    "contact_email" => $email , // include this or contact_id
    "automation" => "9", // one or more
);
$response = $ac->api("automation/contact/add", $post_data);

echo "<pre>";
print_r($response);
echo "</pre>";?>

1 个答案:

答案 0 :(得分:2)

看看这一行: $email = $cart->customer->email .

您在检索电子邮件属性后有一段时间。这会尝试使用返回1的define语句来连接它。所以sample@email.com实际上是sample@email.com1。