1

I'm trying to post json data using cURL to an API script that submits the data to an application. I use file_get_contents('php://input') and the data does not get submitted to the application But if I type in an actual email address in the "contact_email" in the API it submits the email to the application.
Here is the cURL script first:

`

$data = '
 {

 "customer":
     {
      "first_name":"John",
       "last_name":"Smith",
       "email":"[email protected]",
       "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);

`

and here is the API script it posts to:

 <?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>";?>
2
  • What does print_r($_POST) return in your API script? Commented Dec 30, 2014 at 1:38
  • You mean use print_r($_POST) instead of get_file_contents('php//input') ? Commented Dec 30, 2014 at 1:42

1 Answer 1

2

Take a look at this line: $email = $cart->customer->email .

You have a period after the retrieval of the email property. This attempts to concat that with the define statement which would return 1. So [email protected] would actually be [email protected].

Sign up to request clarification or add additional context in comments.

1 Comment

I Removed the period and replaced it with a semi-colon ; and it worked thanks alot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.