Duplosion
Duplosion

Reputation: 269

Trouble with curl and PHP

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": "[email protected]"
 }
}'
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?

Upvotes: 1

Views: 76

Answers (1)

Capri82
Capri82

Reputation: 418

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

Upvotes: 1

Related Questions