简体   繁体   中英

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": "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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM