Home  >  Article  >  Backend Development  >  How to implement php to call the Yunpian.com interface to send text messages

How to implement php to call the Yunpian.com interface to send text messages

不言
不言Original
2018-06-19 09:22:211615browse

This article mainly introduces the relevant information on how to use PHP to call the Yunpian network interface to send text messages. I hope this article can help everyone to realize such a function. Friends in need can refer to it

Implementation method of php calling Yunpian.com interface to send text messages

Yunpian.com sends text messages

/**
   * [SendSms description]
   * @param [string] $tpl_content [发送的短信内容]
   * @param     $send_mobile [发送的手机号码]
   */
  public function SendSms($tpl_content,$send_mobile)
  {
    $this->yunpian_appkey = env('yunpian_appkey');//云片账户appkey
    $this->yunpian_secret = env('yunpianSecretXYZ');//云片账户secret
    //初始化
    $ch    = curl_init();
    $data=array('text'=>$tpl_content,'apikey'=>$this->yunpian_appkey,'mobile'=>$send_mobile);
    curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $send_data = curl_exec($ch);
    curl_close($ch);
    //解析返回结果(json格式字符串)
    $backmassage = json_decode($send_data,true);
    echo '$backmassage ';
    //这里是写在了job里面所以记了个日志可忽略!
    Log::info('smsmessagelog: '.$send_data);
    return [$backmassage,$send_data];
  }

Yunpian.com SMS interface document: http://www.yunpian.com/api2.0/api-domestic/single_send.html

The above is the entire content of this article. I hope it will be helpful to everyone’s study. More related Please pay attention to the PHP Chinese website for content!

Related recommendations:

About the analysis of facade pattern in PHP

Constructor analysis of PHP

The above is the detailed content of How to implement php to call the Yunpian.com interface to send text messages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn