简体   繁体   English

使用Curl通过PHP发送短信

[英]Sending SMS via PHP using Curl

I have this form below that is suppose to send a POST request to the backend, which will in turn send an SMS, on submit: 我在下面有这张表格,假设是要向后端发送POST请求,然后在提交时再发送一条SMS:

<form id="sms" method="post">
      <input type="number" name="mobile" id="mobile" class="text"/>
      <button type="button" name="sub" value="Submit">Send</button>
</form>

I got this from the backend guy with no other details. 我是从后端人员那里得到的,没有其他详细信息。 I found some online tutorials but I couldn't make anything of it. 我找到了一些在线教程,但是我什么也做不了。

curl -X POST \
  https://example.com/gateway/sms \
  -H 'Accept: application/json' \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/json' \
  -H 's: APP' \
  -d '{ 
   "mobile": "1112223333"
 }'

Could anyone point me to a PHP processor that could process this info? 谁能指出我一个可以处理此信息的PHP处理器? My knowledge is very limited. 我的知识非常有限。

Hopefully you can just talk to the backend person, but from what I can tell, they are asking you to create a POST request, using javascript, that does the same thing as the curl command in your example. 希望您可以与后端人员交谈,但是据我所知,他们要求您使用javascript创建POST请求,该请求与示例中的curl命令具有相同的作用。

Your form is not going to work as is, because it will send the request to the backend using an encoded form, but the backend expects JSON. 您的表单无法按原样工作,因为它将使用编码表单将请求发送到后端,但是后端需要JSON。

You need to capture the form submission, and then do something like 您需要捕获表单提交,然后执行类似的操作

url = 'https://example.com/gateway/sms';
data = {mobile: _get_number_from_form() };
success = function(data) { console.log(data); };
headers = {s: 'APP'};
jQuery.ajax({
    type: 'POST',
    url: url,
    data: data,
    success: success,
    dataType: 'json',
    headers: headers
});

postman and the network tab in Chrome will be useful here. Chrome中的邮递员和“网络”标签将在此处有用。

 $inNumber = $_REQUEST["inNumber"];
 $sender = $_REQUEST["sender"];
 $keyword = $_REQUEST["keyword"];
 $content = $_REQUEST["content"];
 $email = $_REQUEST["email"];
 $credits = $_REQUEST["credits"];

// Account details
        $apiKey = urlencode('jpvpyVsTv50-O7te5yiz3oP1DjMkdsiuHSUBS');

        // Message details
        $numbers = $_REQUEST["sender"];
        $sender = urlencode('JHSSVE');

        $text = "Thank you for your order";
        $message = rawurlencode($text);
     $test = true;

        // Prepare data for POST request
        $data = array('apikey' => $apiKey, 'numbers' => $numbers, "sender" => $sender, "message" => $message);

        // Send the POST request with cURL
        $ch = curl_init('https://api.textlocal.in/send/');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);

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

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