Search code examples
phpcurlvonage

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);

When I executed it, the response was null.

but when I access the url on browser, It gives api.txt files which has the response and I get the sms which I just send using that url.


Solution

  • I've tried the same code:

    $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);
    

    And I get the response:

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

    As you can see, the response seems to be populated. So, it seems to work.

    I'd personally recommend using the nexmo-php library since it's officially supported by Nexmo (who I work for).

    $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'
    ]);