PHP return 上的 Nexmo 短信 API false 但不是在浏览器上

Nexmo sms API on PHP return false but not on browser

$url = 'https://rest.nexmo.com/sms/json?api_key=xxx&api_secret=xxx&from=NEXMO&to=xxxxx&text=Welcome+to+Nexmo';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

我执行的时候,响应是空的。

但是当我在浏览器上访问 url 时,它会提供 api.txt 具有响应的文件,我收到了我刚刚使用 url.[=14 发送的短信=]

我试过相同的代码:

$url = 'https://rest.nexmo.com/sms/json?api_key=KEY&api_secret=SECRET&from=NEXMO&to=TO_NUMBER&text=Welcome+to+Nexmo';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

echo($response);

我得到回复:

{
    "message-count": "1",
    "messages": [{
        "to": "TO_NUMBER",
        "message-id": "MESSAGE_ID",
        "status": "0",
        "remaining-balance": "7.25697349",
        "message-price": "0.03330000",
        "network": "23415"
    }]
}%

如您所见,响应似乎已填充。所以,它似乎有效。

我个人推荐使用 nexmo-php 库,因为它得到了 Nexmo(我为之工作)的官方支持。

$client = new Nexmo\Client(new Nexmo\Client\Credentials\Basic(API_KEY, API_SECRET));

$message = $client->message()->send([
    'to' => NEXMO_TO,
    'from' => NEXMO_FROM,
    'text' => 'Test message from the Nexmo PHP Client'
]);