0

laravel アプリケーションで SMS を送信しようとしています。私はinfobipテストアカウントを持っていて、その詳細をSMSに使用していますが、このエラーが発生しています:

ClientException in RequestException.php line 111:
Client error: `POST https://api.infobip.com/sms/1/text/single` resulted in a        `401 Unauthorized` response:
{"requestError":{"serviceException":   {"messageId":"UNAUTHORIZED","text":"Invalid login details"}}}

コード:

$data= '{  
       "from":"InfoSMS",
       "to":"923227124444",
       "text":"Test SMS."
    }';
    $userstring = 'myuserame:password';
    $id =base64_encode ( 'myuserame:password' );
    echo 'Basic '.$id;
    $client = new \GuzzleHttp\Client();
    $request = $client->post('https://api.infobip.com/sms/1/text/single',array(
            'content-type' => 'application/json'
            ),['auth' =>  ['myusername', 'password']]);
    $request->setHeaders(array(
      'accept' => 'application/json',

      'authorization' => 'Basic '.$id,
      'content-type' => 'application/json'
    ));
    $request->setBody($data); #set body!
    $response = $request->send();
    echo $res->getStatusCode(); // 200
    echo $res->getBody();
    return $response;

サイトからダイレクトテキストメッセージを送信しようとしたので、ユーザー名とパスワードは正しいです。

誰かが私が間違っていることを手伝ってくれますか?

ありがとう!

4

2 に答える 2

2

そのため、curl を使用して問題を解決するのに疲れてしまい、他の人のためにコードを配置して動作するようになりました。コード:

$data_json = '{
       "from":"Infobip",
       "to":"9232271274444",
       "text":"test msg."
    }';
    $authorization = base64_encode('username:password');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json',"Authorization: Basic $authorization"));
    //curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_URL, 'https://api.infobip.com/sms/1/text/single');

    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response  = curl_exec($ch);
    //var_dump(curl_getinfo($ch));
    var_dump($response);
    curl_close($ch);*/
于 2016-11-15T09:17:01.747 に答える
1

infobip api 開発者マニュアルを読んでいるように、ユーザー名/パスワードを渡す必要はありません。

これを試して:

$authEncoded = base64_encode('myuserame:password');
$data = array(
    "from" => "InfoSMS",
    "to" => "923227124444",
    "text" => "Test SMS."
);
$request = new Request('POST', 'https://api.infobip.com/sms/1/text/single',
    array(
        'json' => $data,
        'headers' => array(
            'Authorization' => 'Basic ' . $authEncoded,
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
        )
    )
);
$client = new \GuzzleHttp\Client();
$response = $client->send($request);
echo $response->getBody();

今は自分でテストできないので、うまくいったかエラーが発生した場合は最新情報を入手してください.

于 2016-11-14T14:52:08.237 に答える