acidpaul
acidpaul

Reputation: 165

Convert Terminal cURL Call to PHP cURL Request

I am trying to send a cURL request from PHP to ExpressPigeon.com RESTFUL API.

The documentation says that this is how to create contact in a list using cURL command:

curl -X POST -H "X-auth-key: 00000000-0000-0000-0000-000000000000" \
 -H "Content-type: application/json" \
 -d '{"list_id": 11,
      "contacts": [
         {"email": "[email protected]",
          "first_name":"John",
          "last_name": "Doe"
         },
         {"email": "[email protected]",
          "first_name":"Jane",
          "last_name": "Doe"
         }] }' \
 https://api.expresspigeon.com/contacts

Here's what I did:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.expresspigeon.com/contacts');
$fields = array(
            'list_id' => $this->list_code,
            'contacts' => array('email' => $param['email'], 'first_name' => $param['first_name'], 'last_name' => $param['last_name'])
        );
$this->http_build_query_for_curl($fields); //This generates the $this->post_data

curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $this->post_data);

$headers = array(
            'X-auth-key: '.$this->api_key,
            'Content-type: application/json',
            'Content-Length: '.strlen(serialize($post))
        );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$res = (array) json_decode(curl_exec($ch));
curl_close($ch);
print_r($res);

function http_build_query_for_curl( $arrays, $prefix = null )
{
    if ( is_object( $arrays ) ) {
        $arrays = get_object_vars( $arrays );
    }

    foreach ( $arrays AS $key => $value ) {
        $k = isset( $prefix ) ? $prefix . '['.$key.']' : $key;
        if ( is_array( $value ) OR is_object( $value )  ) {
            $this->http_build_query_for_curl( $value, $k );
        } else {
            $this->post_data[$k] = $value;
        }
    }
}

I have this as a result though:

Array
(
    [status] => error
    [code] => 400
    [message] => required Content-type: application/json
)

Upvotes: 0

Views: 486

Answers (2)

acidpaul
acidpaul

Reputation: 165

Support just replied and this is the working code. I've tried it and it works! The only thing I did not do is the the SSL parameters on cURL. Thanks to Hans Z for his revision on my contacts array.

$fields = array(
    'list_id' => $this->list_code,
    'contacts' => array(array('email' => $param['email'], 'first_name' => $param['first_name'], 'last_name' => $param['last_name']))
);
$requestBody = json_encode($fields);
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://api.expresspigeon.com/contacts"); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST,           true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','X-auth-key:'.$this->api_key));
$result = curl_exec($ch); 
curl_close($ch);

Upvotes: 0

Hans Z.
Hans Z.

Reputation: 53948

you need to add one more level of array nesting to achieve the desired JSON output so that contacts is an array instead of an object:

$fields = array(
        'list_id' => $this->list_code,
        'contacts' => array(array('email' => $param['email'], 'first_name' => $param['first_name'], 'last_name' => $param['last_name']))
    );

and then use json_encode on that; you add debug printouts and compare against what you need

Upvotes: 1

Related Questions