2017-06-07 20 views
1

我需要一個幫助。我在使用PHP調用一個API時出錯。使用PHP調用一個sendOTP API時發生錯誤

Error:

Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /opt/lampp/htdocs/test/otp.php on line 8 

Warning: include(http://api.msg91.com/api/sendotp.php?authkey="15529*************"&mobile="9937229853"&message="Your verification code is:1111"&sender="Subhrajyoti"&otp="1111"): failed to open stream: no suitable wrapper could be found in /opt/lampp/htdocs/test/otp.php on line 8 

Warning: include(): Failed opening 'http://api.msg91.com/api/sendotp.php?authkey="15529*************"&mobile="9937229853"&message="Your verification code is:1111"&sender="Subhrajyoti"&otp="1111"' for inclusion (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/test/otp.php on line 8 

我下面解釋我的代碼。

$authkey="15529*************"; 
$mobile=9937229853; 
$code=1111; 
$message="Your verification code is:".$code; 
$sender="Subhrajyoti"; 
$otp=1111; 
include ('http://api.msg91.com/api/sendotp.php?authkey="'.$authkey.'"&mobile="'.$mobile.'"&message="'.$message.'"&sender="'.$sender.'"&otp="'.$otp.'"'); 

這裏我試圖發送OTP,但得到這些消息。在這裏,我需要發送OTP並獲得響應。請幫幫我。

+0

我不明白爲什麼這包括可以使用捲曲 – lalithkumar

+0

這'url'是隻送了OTP API。 – satya

回答

1

可以使用CURLfile_get_contents

$response = file_get_contents('http://api.msg91.com/api/sendotp.php?authkey="'.$authkey.'"&mobile="'.$mobile.'"&message="'.$message.'"&sender="'.$sender.'"&otp="'.$otp.'"'); 
$response = json_decode($response); 
使用捲曲

或 :

$ch = curl_init(); 
$data=array('authkey'=>$authkey,'mobile'=>$mobile,'message'=>$message);//all parameter 
curl_setopt($ch, CURLOPT_URL, 'http://api.msg91.com/api/sendotp.php'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
$result = curl_exec($ch); 
print_r($result); 
curl_close($ch); 
+0

我現在使用'file_get_contents.'現在我得到消息'{「消息」:「OTP應該是數字」,「類型」:「錯誤」}'。在這裏我傳遞otp作爲數字。 – satya

+0

只需檢查url和參數.file_get_contents將執行url並獲取文件內容或回覆 – lalithkumar

+0

只需確保url參數中是否有空格以避免此問題需要使用'urlencode()' – lalithkumar

0

而是這行:

include ('http://api.msg91.com/api/sendotp.php?authkey="'.$authkey.'"&mobile="'.$mobile.'"&message="'.$message.'"&sender="'.$sender.'"&otp="'.$otp.'"'); 

使用cURL打與消息和手機號碼附加的URL,從它那裏得到響應。並且不要忘記使用urlencode()函數對整個網址進行編碼。

例:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$APIresponse = curl_exec($ch); 
curl_close($ch); 
$res = json_decode($APIresponse, true); 
+0

你可以請你分享你的想法,我有點困惑。 – satya

+0

'$ postData'包含的內容。 – satya

+0

只需檢查更新的答案,它很容易實現。 –

相關問題