大家好,我正在嘗試運行一個使用基于 URL 的身份驗證的 API 請求。但是每當訊息在訊息文本之間有一些空格時,我就會收到錯誤
無法打開流:HTTP 請求失敗!HTTP/1.1 400 Bad Request in C:\wamp64\www\testscripts\wialon.php 第 21 行
當我使用相同的 url 字串(訊息中間有空格)并在瀏覽器上運行它時,訊息發送成功,下面是我的代碼
<?php
$sendname=$_GET['name'];
$sendage=$_GET['age'];
$passdat=$_GET['password'];
$url = 'https://api.smsleopard.com/v1/sms/send?username=O6QDPDmFBDu6BYBTTX3q&password=.$passdat&message='.$sendage.'&destination=254700160125&source=TWIGA';
$contents = file_get_contents($url);
//If $contents is not a boolean FALSE value.
if($contents !== false){
//Print out the contents.
echo $contents;
}
exit();
}
uj5u.com熱心網友回復:
Php 有一個內置函式來構建正確轉義的查詢字串,我會使用它
https://www.php.net/manual/en/function.http-build-query.php
<?php
$sendname = $_GET['name'];
$sendage = $_GET['age'];
$passdat = $_GET['password'];
$url = 'https://api.smsleopard.com/v1/sms/send?'
. http_build_query([
'username' => 'O6QDPDmFBDu6BYBTTX3q',
'password' => $passdat,
'message' => $sendage,
'destination' => '254700160125',
'source' => 'TWIGA',
]);
$contents = file_get_contents($url);
//If $contents is not a boolean FALSE value.
if ($contents !== false) {
//Print out the contents.
echo $contents;
}
exit();
uj5u.com熱心網友回復:
$url = 'https://api.smsleopard.com/v1/sms/send?username=O6QDPDmFBDu6BYBTTX3q&password='.$passdat.'&message='.$sendage.'&destination=254700160125&source=TWIGA';
你忘記了多次
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/386966.html
