在php中使用api发送SMS

在php中使用api发送SMS,php,Php,我正在尝试使用BulkSMS api发送短信,我从中获得了一个帐户。现在,每次我传入一条文本以及我试图发送到的号码,我都会收到一个400状态=错误的请求 在我的代码中: 避免向服务器发送重复的sms function cut_id_duplication() { return 0; } 获取以数组形式发送的字段 function post_body( $post_fields ) { $id = cut_id_duplication(); if ( $id > 0

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

在我的代码中:

避免向服务器发送重复的sms

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;
}
用于发送sms和状态的功能:例如“”

Curl脚本代码及其设置

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;
}
存储发送消息的数组变量

   $send_out_message = array(
   'from'  => $sms_from,
   'to'    => $sms_to,
   'body'  => $message_body
  );
调用send_messages()函数


你试过询问服务提供商吗?毕竟这是他们的服务,所以他们应该能够看到发生了什么,以及他们的服务应该如何使用。所以不是一个通用的“internet上的每项服务”—helpdesk。是的,我有,但我得到了一个查找服务器错误的链接“错误请求-表示请求有问题;通常是属性或参数的验证错误。错误代码是400错误请求。”。因此,您可能传递了错误的参数或格式无效?这不是一个好问题,因为它基本上是关于“如何使用此服务?”。如果您无法找出问题所在,请再次联系他们并再次解释您的问题。
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;
}
   $send_out_message = array(
   'from'  => $sms_from,
   'to'    => $sms_to,
   'body'  => $message_body
  );
  $send_collective = send_messages( json_encode( $send_out_message ), 'messages' );

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