使用php中的api发送短信

时间:2018-07-27 11:12:39

标签: php

我正在尝试使用BulkSMS api发送一条短信,我从中获得一个帐户。现在,每次我传递文本以及要发送的号码时,都会收到400状态=错误的请求。

在我的代码中:

$sms_to;
$sms_from;
$message_body;

$base_url;
$cUrl;
$http_status;

避免将重复的短信发送到服务器

function cut_id_duplication() {
   return 0;
}

获取以数组形式发送的字段

function post_body( $post_fields )
{
    $id = cut_id_duplication();

    if ( $id > 0 )
    {
        $post_fields['id'] = cut_id_duplication();
    }

    $post_fields = '';

    foreach ( $post_fields as $key => $value )
    {
        $post_fields .= urlencode( $key ). '=' . urlencode( $value ) . '&';
    }

    $post_fields = rtrim( $post_fields, '&' );

    return $post_fields;
}

用于发送短信以及状态的函数,例如'https://api.bulksms.com/v1/messages'

function send_messages( $msg, $status )
{
   return curl_function( $msg, $status );
}

卷曲脚本代码及其设置

private function curl_function( $post_values, $function = '')
{
    $base_url = 'https://api.bulksms.com/v1/'.$function;

    $cUrl = curl_init();

    $method = '';

    if ( $method == null )
    {
        $method == "GET";
    }
    else
    {
        $method == "POST";
    }

    $post_fields = post_body( $post_values );

    curl_setopt( $cUrl, CURLOPT_URL, $base_url  ); 
    curl_setopt( $cUrl, CURLOPT_CUSTOMREQUEST, $method );
    curl_setopt( $cUrl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $cUrl, CURLOPT_POSTFIELDS, $post_fields );
    curl_setopt( $cUrl, CURLOPT_SSL_VERIFYPEER, false);

     $output = curl_exec( $cUrl );

     $http_status = curl_getinfo( $cUrl, CURLINFO_HTTP_CODE );

     if ( $http_status == 400 ) 
     {
         echo ("Return code is {$http_status}: Bad Request");
         return false;
     }

     if(curl_errno( $cUrl ))
         echo curl_errno( $cUrl );

     if(curl_error($cUrl))
         echo curl_error( $cUrl );

     curl_close( $cUrl );

     return $output;
}

数组var来存储send-out-msg

   $send_out_message = array(
   'from'  => $sms_from,
   'to'    => $sms_to,
   'body'  => $message_body
  );

调出send_messages()函数

  $send_collective = send_messages( json_encode( $send_out_message ), 'messages' );

        if ( !$send_collective )
        {
            echo( 'ERROR SENDING OUT SMS.' );
            exit();
        }

0 个答案:

没有答案