简体   繁体   English

卷曲和PHP的麻烦

[英]Trouble with curl and PHP

I'm trying to work with an API. 我正在尝试使用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. 当我var_dump $ result时,我得到一个空字符串。

Do you see what I'm doing wrong? 看到我在做什么错吗?

Change you $params array into PHP Json_encode array like this: 将您的$ params数组更改为PHP Json_encode数组,如下所示:

$params = json_encode(you array); $ params = json_encode(您数组);

Also here is the official SENDWITHUS API implementation code in PHP. 这里也是PHP的官方SENDWITHUS API实现代码。

SendWithUs PHP code SendWithUs PHP代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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