繁体   English   中英

使用php curl调用api?

[英]Calling api using php curl?

我正在调用一个短信网关 api,下面是我的代码。

$otp="1234";
$mobile_number=1234567890;
$username = 123456;
$hash = 1234;
$from = "SENDER";
$message="Your one time password is ".$otp."";

$payload = "username=".$username."&password=".$hash."&to="."91".$mobile_number."&from=".$from."&text=".$message;
$ch = curl_init('https://sms.zestwings.com/smpp.sms?');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch); // This is the result from the API
curl_close($ch);
echo json_encode($result);
return $result;

当我使用用户名、密码、发件人、电话号码等所有详细信息在浏览器中点击 url 时,OTP 被发送到我的手机号码,当我尝试使用 curl 方法时,它给了我如下错误消息:

注意:未定义索引:第 232 行 /var/www/html/api/index.php 中的用户名

注意:未定义索引:密码在 /var/www/html/api/index.php 第 232 行 User account Deactivated

我在服务器中没有任何 index.php 页面,只有 otp.php 页面存在,任何人都可以告诉我出了什么问题。

我修改了我的代码,现在可以正常工作了。

$mobile_number=1234567890;
$from = "SENDER";
$message=urlencode("Hello");
$url='https://sms.zestwings.com/smpp.sms?username=123456&password=123&to=91'.$mobile_number.'&from='.$from.'&text='.$message.'';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM