1

I'm trying to work with an API. This is the example that's been provided to me in the documentation:

curl \
-X POST \
-u YOUR_API_KEY: \
-d '{
"email_id": "YOUR_EMAIL_ID",
"recipient": {
     "address": "some.one@email.com"
 }
}'
https://api.sendwithus.com/api/v1/send

This is my attempt at that:

$url = 'https://api.sendwithus.com/api/v1/send';
$user = 'my_api_key';

$params = array(
   'email_id' => 'my_email_id',
   'recipient' => array(
        'address' => 'my_email_address'
    )
);


$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_USERPWD, $user. ':' . '');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
curl_close ($ch);

When I var_dump $result, I get an empty string.

Do you see what I'm doing wrong?

bvanvugt
  • 1,212
  • 1
  • 8
  • 15
Duplosion
  • 255
  • 3
  • 10
  • 2
    the example uses json, and your version doesn't. you're sending normal `foo=bar` key/value pairs as if it was a normal form submission. you need json_encode() in the POSTFIELDS. – Marc B Feb 09 '15 at 16:01

1 Answers1

1

Change you $params array into PHP Json_encode array like this:

$params = json_encode(you array);

Also here is the official SENDWITHUS API implementation code in PHP.

SendWithUs PHP code

Community
  • 1
  • 1
Capri82
  • 418
  • 6
  • 24
  • I just tried that, but still got the same result. Here's the modified code I used: $params = json_encode(array( 'email_id' => 'my_api_key', 'recipient' => array( 'address' => 'my_email_address' ) )); – Duplosion Feb 09 '15 at 16:13
  • I'll give their client another shot. I was trying to avoid it, because the composer.json they provided seems to need some love and that's a whole other learning curve for me. – Duplosion Feb 09 '15 at 16:14