【问题标题】:Integrating SMS api with woocommerce , Not sending messages将 SMS api 与 woocommerce 集成,不发送消息
【发布时间】:2015-12-22 12:54:56
【问题描述】:

我正在将 SMS API 与 WooCommerce 集成,以便在客户在网站上进行任何购买时自动向手机发送订单更新。

下面是我的代码

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($billing_phone)
{
$username = "my username";
$hash = "d761fbd7bd31c5eeec2a5b2556d6b9d3b1a1ae51";
//Multiple mobiles numbers separated by comma
$mobileNumber = "$billing_phone";


$senderId = "ORNGMT";
$message = urlencode("Dear Customer");
$postData = array(
    'hash' => $hash,
    'mobiles' => $$billing_phone,
    'message' => $message,
    'sender' => $senderId,

);
$url='http://api.textlocal.in/send/?';

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postData
    //,CURLOPT_FOLLOWLOCATION => true
));

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$output = curl_exec($ch);

if(curl_errno($ch))
{
    echo 'error:' . curl_error($ch);
}

curl_close($ch);

echo $output;
}

正确吗? 我已将此代码添加到 functions.php 页面

我的短信网关提供商已向我发送以下示例代码以使用 PHP 发送短信

<?php
// Authorisation details.
$username = "your login id";
$hash = "your hash key";

// Configuration variables. Consult http://api.textlocal.in/docs for more info.
$test = "0";

// Data for text message. This is the text message data.
$sender = "API Test"; // This is who the message appears to be from.
$numbers = "44777000000"; // A single number or a comma-seperated list of numbers
$message = "This is a test message from the PHP API script.";
// 612 chars or less
// A single number or a comma-seperated list of numbers
$message = urlencode($message);
$data = "username=".$username."&hash=".$hash."&message=".$message."&sender=".$sender."&numbers=".$numbers."&test=".$test;
$ch = curl_init('http://api.textlocal.in/send/?');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch); // This is the result from the API
curl_close($ch);

?>

【问题讨论】:

  • “不工作”是什么意思?哪部分不合格?你有没有采取任何步骤来找到失败点?现在您要求我们为您调试代码。
  • 它不是在发送消息。
  • 仍然建议您单步执行代码并 echo/var_dump 各个部分,以便您可以看到失败的地方。 PS-我还建议看看 [wp_remote_post()]codex.wordpress.org/Function_Reference/wp_remote_post)
  • 例如,乍一看,我可以在一行中看到$mobileNumber = "$billing_phone";,然后是'mobiles' =&gt; $$billing_phone,。您确定以 API 接受的格式传递有效电话号码吗?
  • 不,妈妈!我不确定。 :( api.textlocal.in/docs/sendsms > 这是我正在使用的 api。我对此没有任何先验知识,所以从最近几天开始搜索它。

标签: wordpress woocommerce


【解决方案1】:

为每个事件集成消息 API 会很困难,您需要在代码级别自定义。

您可以为 wordpress woocommerce 使用流行的 SMSplugin

https://wordpress.org/plugins/woocommerce-apg-sms-notifications/

您只需要从 woocommerce 管理员登录配置此插件,它就会自动发送短信通知。我们正在使用它与Spring Edge sms gateway.. 工作良好。

【讨论】:

    【解决方案2】:

    经过测试和工作 自定义 api 与 Woocommerce wordpress 集成

    Ufone 巴基斯坦短信与 woocommerce wordpress 集成

    如果您正在寻找与 ufone pakistan sms api bsms ufone pakistan 服务提供商与 woocommerce wordpress 的集成,请在您的函数文件中使用以下代码

    sms api 将 ufone bsms 与 wordpress woocommerce 集成感谢此页面上的作者 将自定义 SMS API 与 woocommerce 集成

    //add this line for calling your function on creation of order in woocommerce 
    add_action('woocommerce_order_status_processing', 'custom_func', 10, 3);
    
    function custom_func ($order_id) {
    
    $order_details = new WC_Order($order_id);
    
    //fetch all required fields
    $billing_phone = $order_details->get_billing_phone();
    $billing_name = $order_details->get_billing_first_name();
    $billing_last_name = $order_details->get_billing_last_name();
    $total_bill = $order_details->get_total();
    
    $textmessage = rawurlencode("Dear $billing_name, Thank you for your order. Your order #$order_id is being processed. Please wait for confirmation call Total Bill = $total_bill");
    
    // Now put HTTP ufone BSMS API URL
    $url = "https://bsms.ufone.com/bsms_v8_api/sendapi-0.3.jsp?id=msisdn&message=$textmessage&shortcode=SHORTCODE&lang=English&mobilenum=$billing_phone&password=password&messagetype=Nontransactional";
    
    // NOW WILL CALL FUNCTION CURL  
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch);
    $err = curl_error($ch);
    curl_close($ch);
    
    return $order_id;
    }
    

    【讨论】:

      【解决方案3】:

      你的函数中的$billingphone 是 ID。试试下面的代码。有效

      add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
      function custom_process_order($order_id) {
      //Lets get data about the order made
      $order = new WC_Order( $order_id );
      
      //Now will fetch customer/buyer id here
      $customer_id = $order->user_id;
      
      //now finally we fetch phone number 
      $billing_phone = get_user_meta( $customer_id, 'billing_phone', true );
      
      // Now put your HTTP SMS API URL . I PUT WHICH WE ARE USING
      $jsonurl = "http://tsms.thirdeyegoa.com/api/sendmsg.php?user=USERNAME&pass=PASSWORD&sender=MYSENDERID&phone=".$billing_phone."&priority=ndnd&stype=normal&text=MY MESSAGE TO CUSTOMER.";
      
      // NOW WILL CALL FUNCTION CURL  
      $json = curl($jsonurl);
      
      return $order_id;
      }
      

      您也可以使用相应的钩子对 Order Completed 执行相同的操作。

      【讨论】:

      • 感谢您的回复,您能帮我的api吗?它不工作。
      • 您的 api 可能会有所不同,请联系您的短信提供商。但这就是您如何获取用户的计费电话号码并向他们发送短信的方式。如果您想要带有简单 api 的短信包,请联系短信提供商。这与我使用的方式相同,并且效果很好。您可以将它用于订单完成、处理、付款完成等挂钩。将此代码放在functions.php中,您可以转储结果并检查您是否会得到它。
      • 我已经发布了他们提供给我的代码示例。
      • 好的,然后检查卷曲。服务提供者 API Url 是需要 POST 还是直接点击链接
      • 那么请按照他们的使用sms api的文档。谢谢
      【解决方案4】:

      我写了这段代码,它正在工作。

      add_action('woocommerce_order_status_completed','payment_complete');
        function payment_complete($order_id)
      {
      
      $order=new Wc_order($order_id);
      $order_meta = get_post_meta($order_id);
      $shipping_first_name = $order_meta['_shipping_first_name'][0];
      $phone=$order_meta['_billing_phone'][0];
      $first_name=$order->billing_first_name;
      $last_name=$order->billing_last_name;
      $name=$first_name."".$last_name;
      $mobile=$order->billing_mobile;
      $items = $order->get_items();
      foreach ( $items as $item ) {
      $product_name = $item['name'];
      $product_id = $item['product_id'];
      $product_variation_id = $item['variation_id'];
      $time=$item['timings'];
      $slot=$item['time_slot'];
      $qty= $item['qty'];
      $date=$item['tour_date'];
      }
      $text="your message here";
      $data="user=username&pass=password&sender=sendername&phone=".$phone."&priority=ndnd&stype=normal&text=".$text;
      $ch = curl_init('http://bhashsms.com/api/sendmsg.php?');
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $result = curl_exec($ch);
       echo $result; 
      curl_close($ch);
      }
      

      【讨论】:

        【解决方案5】:

        我来自 Textlocal。让我帮你写代码。

        首先,您收到的错误 - “意外令牌”通常是由于 JSON 响应无效。如果您不知道问题的根源,我建议您查看开发部分的 Javascript 控制台(触发错误)。作为参考,您可以查看 Mike 的博客here

        在您共享的代码中,Textlocal API 未接收成功 POST 调用所必需的参数。你可以这样改写你的函数:

        add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
        function custom_process_order($order_id) {
        
        $order = new WC_Order( $order_id );
        $customer_id = $order->user_id; 
        $billing_phone = get_user_meta( $customer_id, 'billing_phone', true );
        
        $data="username=USERNAME&hash=hash&sender=ORNGMT&numbers=".$billing_phone."&message=MY MESSAGE TO CUSTOMER.";
        $jsonurl = curl_init('https://api.textlocal.in/send?');
        
        $json = curl($jsonurl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($json);
        echo $result; 
        curl_close($json);
        
        return $order_id;
        }
        

        如果您需要任何进一步的帮助,请联系我们的支持团队 (support@textlocal.in)

        【讨论】:

          猜你喜欢
          • 2018-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多