Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Nahid_nz's avatar

Sending SMS with nexmo

I am trying to build up a sms system from my laravel project, But i have got a problem.I dont understand yet now, Anyone help me for that. When i run my url, it shows these error : RequestException in Promise.php line 135: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

0 likes
3 replies
Nahid_nz's avatar

Thanks a lot, But a small error happend. That is, "Request in Client.php line 75: Missing username" I go to the client.php directory but don't get username anywhere.....

Nahid_nz's avatar

namespace Nexmo\Message;

use Nexmo\Client\ClientAwareInterface; use Nexmo\Client\ClientAwareTrait; use Nexmo\Client\Exception; use Zend\Diactoros\Request;

class Client implements ClientAwareInterface { use ClientAwareTrait;

public function send($message)
{
    if(!($message instanceof MessageInterface)){
        $message = $this->createMessageFromArray($message);
    }

    $params = $message->getRequestData(false);
    
    $request = new Request(
        \Nexmo\Client::BASE_REST . '/sms/json'
        ,'POST',
        'php://temp',
        ['content-type' => 'application/json']
    );

    $request->getBody()->write(json_encode($params));
    $message->setRequest($request);
    $response = $this->client->send($request);
    $message->setResponse($response);

    //check for valid data, as well as an error response from the API
    $data = $message->getResponseData();
    if(!isset($data['messages'])){
        throw new Exception\Exception('unexpected response from API');
    }

    //normalize errors (client vrs server)
    foreach($data['messages'] as $part){
        switch($part['status']){
            case '0':
                continue; //all okay
            case '1':
                if(preg_match('#\[\s+(\d+)\s+\]#', $part['error-text'], $match)){
                    usleep($match[1] + 1);
                } else {
                    sleep(1);
                }

                return $this->send($message);
            case '5':
                $e = new Exception\Server($part['error-text'], $part['status']);
                $e->setEntity($message);
                throw $e;
            default:
                $e = new Exception\Request($part['error-text'], $part['status']);
                $e->setEntity($message);
                throw $e;
        }
    }

    return $message;
}


public function get($query)
{
    if($query instanceof Query){
        $params = $query->getParams();
    } else if($query instanceof MessageInterface){
        $params = ['ids' => [$query->getMessageId()]];
    } else if(is_string($query)) {
        $params = ['ids' => [$query]];
    } else if(is_array($query)){
        $params = ['ids' => $query];
    } else {
        throw new \InvalidArgumentException('query must be an instance of Query, MessageInterface, string ID, or array of IDs.');
    }

    $request = new Request(
        \Nexmo\Client::BASE_REST . '/search/messages?' . http_build_query($params),
        'GET',
        'php://temp',
        ['Accept' => 'application/json']
    );

    $response = $this->client->send($request);
    $response->getBody()->rewind();
    $data = json_decode($response->getBody()->getContents(), true);

    if($response->getStatusCode() != '200' && isset($data['error-code'])){
        throw new Exception\Request($data['error-code-label'], $data['error-code']);
    } elseif($response->getStatusCode() != '200'){
        throw new Exception\Request('error status from API', $response->getStatusCode());
    }

    if(!isset($data['items'])){
        throw new Exception\Exception('unexpected response from API');
    }

    if(count($data['items']) == 0){
        return [];
    }

    $collection = [];

    foreach($data['items'] as $index => $item){
        switch($item['type']){
            case 'MT':
                $new = new Message($item['message-id']);
                break;
            case 'MO':
                $new = new InboundMessage($item['message-id']);
                break;
            default:
                throw new Exception\Exception('unexpected response from API');
        }

        $new->setResponse($response);
        $new->setIndex($index);
        $collection[] = $new;

    }

    return $collection;
}

public function search($idOrMessage)
{
    if($idOrMessage instanceof MessageInterface){
        $id = $idOrMessage->getMessageId();
        $message = $idOrMessage;
    } else {
        $id = $idOrMessage;
    }

    $request = new Request(
        \Nexmo\Client::BASE_REST . '/search/message?' . http_build_query(['id' => $id]),
        'GET',
        'php://temp',
        ['Accept' => 'application/json']
    );

    $response = $this->client->send($request);

    $response->getBody()->rewind();
    
    $data = json_decode($response->getBody()->getContents(), true);

    if(!$data){
        throw new Exception\Request('no message found for `' . $id . '`');
    }

    if($response->getStatusCode() != '200' && isset($data['error-code'])){
        throw new Exception\Request($data['error-code-label'], $data['error-code']);
    } elseif($response->getStatusCode() != '200'){
        throw new Exception\Request('error status from API', $response->getStatusCode());
    }


    switch($data['type']){
        case 'MT':
            $new = new Message($data['message-id']);
            break;
        case 'MO':
            $new = new InboundMessage($data['message-id']);
            break;
        default:
            throw new Exception\Exception('unexpected response from API');
    }

    if(isset($message) && !($message instanceof $new)){
        throw new Exception\Exception(sprintf(
            'searched for message with type `%s` but message of type `%s`',
            get_class($message),
            get_class($new)
        ));
    }

    if(!isset($message)){
        $message = $new;
    }

    $message->setResponse($response);
    return $message;
}

protected function createMessageFromArray($message)
{
    if(!is_array($message)){
        throw new \RuntimeException('message must implement `' . MessageInterface::class . '` or be an array`');
    }

    foreach(['to', 'from'] as $param){
        if(!isset($message[$param])){
            throw new \InvalidArgumentException('missing expected key `' . $param . '`');
        }
    }

    $to = $message['to'];
    $from = $message['from'];

    unset($message['to']);
    unset($message['from']);

    return new Message($to, $from, $message);
}

public function __call($name, $arguments)
{
    if(!(strstr($name, 'send') !== 0)){
        throw new \RuntimeException(sprintf(
            '`%s` is not a valid method on `%s`',
            $name,
            get_class($this)
        ));
    }

    $class = substr($name, 4);
    $class = 'Nexmo\\Message\\' . ucfirst(strtolower($class));

    if(!class_exists($class)){
        throw new \RuntimeException(sprintf(
            '`%s` is not a valid method on `%s`',
            $name,
            get_class($this)
        ));
    }

    $reflection = new \ReflectionClass($class);
    $message = $reflection->newInstanceArgs($arguments);

    return $this->send($message);
}

}

Please or to participate in this conversation.