2016-11-14 56 views
0

我想用我的laravel應用程序發送短信。我有infobip測試帳戶,並使用該信息來短信發短信,但收到此錯誤:infraip單人短信服務在laravel中使用guzzle

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"}}} 

CODE:

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

的用戶名和pssword是正確的,因爲我試圖發送直接短信該網站及其在那裏工作。

任何人都可以幫我,我做錯了什麼?

謝謝!

回答

1

所以我累了用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);*/ 
1

你不需要通過用戶名/密碼,因爲你read in the infobip api developer manuals

試試這個:

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

我無法測試它自己現在這樣保持它的工作我更新,或者你會得到一個錯誤。

+0

我在這裏得到了請求錯誤,但我累了用捲曲來解決它。在這裏放置代碼。 –