PHP Curl发送短信

时间:2017-02-26 19:36:40

标签: php api curl while-loop sms

我几乎处于PHP和cURL的初学者级别。 该问题与 php CronJob脚本有关,它从phpmyadmin数据库中获取与具有特定状态的客户订单相关的数据(例如" 0",这意味着SMS未发送/或否更新发送给客户)并更新送货详细信息。

在添加代码以通过我的SMS服务提供商通过API发送短信之前,脚本工作正常,用于发送电子邮件,关于发货更新,然后更新数据库,如订单状态和其他必要的细节给客户以及系统。

  

出现的问题是,假设100-150的结果被提取为   状态=' 0'的数组循环只执行一次并停止,而   在添加SMS代码之前,它用于运行 n 数字行   获取状态=' 0'。

我已经尝试了 StackOverflow,PHP手册,W3C 以及开发人员列出的几乎所有方法,但我仍然无法让所有方法成功运行得到的结果。

我想要的东西 -  的 1。从状态=' 0'的表格中选择数据。 **✓  2.使用mysqli_fetch_array获取有关订单的详细信息。  3.按照代码中的格式发送电子邮件。  4.更新订单历史记录和订单状态。  5.根据规定的模板使用API​​发送短信。 (⍻不工作)**

<?php

$con=mysqli_connect("localhost","admin","pass","database");//database connection
$get_email_contents = "SELECT * FROM `oc_shipping` WHERE `status`='0'";
date_default_timezone_set("Asia/Kolkata");
$date = date('Y-m-d');
if($data = mysqli_query($con,$get_email_contents))
{
while ($row=mysqli_fetch_array($data))
{

$order_id = $row['order_id'];
$tracking_no = $row['tracking_awb'];


$get_email = "SELECT * FROM `oc_order` WHERE `order_id`='$order_id'";
if($email_data = mysqli_query($con,$get_email))
{
while ($email_row=mysqli_fetch_array($email_data))
{
$email = str_replace(" ","",$email_row['email']);
$firstname = ucwords($email_row['firstname']);
$lastname = ucwords($email_row['lastname']);
$received_on = $email_row['date_added'];
$date_added = date("d M Y",strtotime($received_on));
$address_1 = $email_row['shipping_address_1'];
$address_2 = $email_row['shipping_address_2'];
$city = $email_row['shipping_city'];
$pincode = $email_row['shipping_postcode'];
$total = str_replace(".0000","",$email_row['total']);
$mobile = $email_row['telephone'];

$delivery_date = date('d-M-Y', strtotime($date. ' + 7 days'));
$track_url = "https://track.delhivery.com/p/".$tracking_no;
$text = "ABC.com
Hi ".$firstname.", Your Order ".$order_id." has been shipped via XYZ AWB #".$tracking_no.", which can be tracked on ".$track_url."
Expected delivery date is ".$delivery_date.". Please keep ready Rs.".$total."/- on the date of delivery.
Thank you for shopping on ABC.com";
echo "SMS Sent : ".$text."<br><br>";
$encode= rawurlencode($text);

//Just for my reference
$url= "login.smshub.com/api/mt/SendSMS?APIKey=****&senderid=ABCABC&channel=2&DCS=0&Route=1&Flashsms=0&Number=$mobile&Text=$encode";


$ch = curl_init("login.smshub.com/api/mt/SendSMS?APIKey=****&senderid=ABCABC&channel=2&DCS=0&Route=1&Flashsms=0&Number=$mobile&Text=$encode");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,"");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
$data = curl_exec($ch);
$json = json_decode($data);
$JobID = $json->{'JobId'};
$Error = $json->{'ErrorMessage'};

echo "Status : <strong>$JobID $Error </strong><br>";  

//Email Receiver//
   $to = "$email";

   //From Header//
   $header = "From: ABC.com<shipping@example.com>"."\r\n";
   $header .= "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html; charset=ISO-8859-1" . "\r\n";

   // Subject //
   $subject = "Your Order #$order_id has been Shipped";

   // Message //
   $message = "<html>
<head>
    <title></title>
    <link href='https://fonts.googleapis.com/css?family=Pacifico|Varela+Round' rel='stylesheet' />
</head>
<body>
<table align='center' cellpadding='1' style='background-color:#ffffff; border-color:#f3f3f3; border-radius:10px; border-size:1px; border-style:outset; max-width: 100%; width:100%; font-family: Helvetica,Arial,sans-serif; '>
    <tbody>
        <tr>
            <td colspan='2' style='text-align:center'>
            <p style='text-align:center'><a href='example.com' target='_blank'><img alt='abc.com' height='63' src='example.com/image/shipping/Logo.gif' width='250' /></a></p>
            </td>
        </tr>
        <tr>
            <td style='border-color:#f3f3f3; text-align:center; white-space:nowrap; width:50%'>
            <p>Thank you for shopping on abc</p>

            <h3 style='text-align:left;margin-left: 40px;'>Order Details</h3>

            <table align='center' border='0' cellpadding='5' cellspacing='5' style='background-color:#f2f2f2; border-radius:5px; color:#333333; width:85%;'>
                <tbody>
                    <tr>
                        <td>
                        <p style='text-align:left'>Order Number: <strong>$order_id</strong></p>

                        <p style='text-align:left'>Date Received: <strong>$date_added</strong></p>

                        <p style='text-align:left'>Name: <strong>$firstname $lastname</strong></p>
                        </td>
                    </tr>
                </tbody>
            </table>


            </td>
        </tr>
        <tr>
            <td colspan='2' style='border-color:#f3f3f3; text-align:center'>&nbsp;</td>
        </tr>
        <tr>
            <td colspan='2' style='border-color:#f3f3f3; text-align:center'>
            <h3 style='text-align:left;margin-left: 40px;'>Delivery Address</h3>

            <table align='center' border='0' cellpadding='5' style='background-color:#f2f2f2; border-radius:5px; color:#333333; padding:7px; width:85%;'>
                <tbody>
                    <tr>
                        <td>
                        <p style='text-align:left'><strong>$address_1</strong></p>

                        <p style='text-align:left'><strong>$address_2</strong></p>

                        <p style='text-align:left'><strong>$city - $pincode</strong></p>

                        <p style='text-align:left'><strong>Mobile No. $mobile</strong></p>
                        </td>
                    </tr>
                </tbody>
            </table>

            <p>&nbsp;</p>
            </td>
        </tr>
        <tr>
            <td colspan='2' style='text-align:center'>
            <p style='text-align:center'><img alt='Order Shipped' height='90' src='example.com/image/shipping/Shipping.jpg' width='410' /></p>

            <p>Your order has been shipped from our warehouse.</p>

            <p>Amount Payable</p>

            <h2>Rs.<strong>$total</strong></h2>
            </td>
        </tr>
        <tr>
            <td colspan='2' style='text-align:center;'>
            <table align='center' border='0' cellpadding='1' cellspacing='1' style='background-color:#000000; border-radius:5px; color:#ffffff; font-size:larger; font-weight:700; padding:10px 15px; text-align:center; text-decoration:none; width:85%;'>
                <tbody>
                    <tr>
                        <td><a href='https://track.xyz.com/p/$tracking_no' target='_blank' style='padding:15px 25px; text-align:center; text-decoration:none; color:#FFF; text-decoration:none;'>TRACK PACKAGE</a></td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
        <tr>
            <td colspan='2' style='text-align:center'>
            <p><br />
            &nbsp;</p>
            </td>
        </tr>
        <tr>
            <td colspan='2'>
            <p style='text-align:center'><em><span style='font-size:12px'>Instructions</span></em></p>

            <table align='center' border='0' cellspacing='5' style='background-color:#f2f2f2; border-radius:5px; color:#333333; width:85%;'>
                <tbody>
                    <tr>
                        <td><span style='font-size:12px'>1.Please do not accept the package, if it is damaged / tampered before delivery.</span></td>
                    </tr>
                    <tr>
                        <td><span style='font-size:12px'>2.Your freebies (if any) shall be inside the package.</span></td>
                    </tr>
                    <tr>
                        <td><span style='font-size:12px'>3.Please mention remarks while signing for delivery, if you doubt that the package might be damaged or is suspicious.</span></td>
                    </tr>

                </tbody>
            </table>
            </td>
        </tr>
        <tr>
            <td colspan='2' style='text-align:center'>
            <p>&nbsp;</p>

            <p><span style='font-size:12px; color:#a9a9a9;'>Note: We do not demand your bank or credit card details over phone. Please do not divulge these details to fraudsters claiming to be calling on behalf of abc.&nbsp;</span></p>

            <p>&nbsp;</p>
            </td>
        </tr>
        <tr>
            <td colspan='2' style='text-align:center'>
            <p>Contact Us</p>

            <p><strong><a href='mailto:help@abc.com?subject=Regarding%20Order%20ID%20$order_id' style='text-decoration:none; color:#000;'>help@example.com</a></strong></p>

            <p><span style='font-size:10px; color:#a9a9a9;'>Do not reply to this email, this is an automated email sent to $email.</p>
            </td>
        </tr>
    </tbody>
</table>

</body>
</html> \r\n";


   //Send Mail//
   $mail_send = mail($to,$subject,$message,$header);

   if($mail_send)
   {

  $con=mysqli_connect("localhost","admin","pass","database");//database connection
   $delete = "UPDATE `oc_ship` SET `status`='1' WHERE `order_id`='$order_id'";
   if($deleted = mysqli_query($con,$delete))
   {
   $update_comment = mysqli_real_escape_string($con,"<p>Your order has been shipped via XYZ AWB #<b>$tracking_no</b>.</p> <p><a href='track.xyz.com/p/$tracking_no' target='_blank'>Click here to track your order</a></p>");
  $date_time = date('Y-m-d H:i:s');
   $order_history = "INSERT INTO `oc_order_hist`(`order_id`,`order_status_id`,`notify`,`comment`,`date_added`) VALUES ('$order_id','3','1','$update_comment','$date_time')";
   if($order_history_update = mysqli_query($con,$order_history))
   {
  $order_status = "UPDATE `oc_order` SET `order_status_id`='3' WHERE `order_id`='$order_id'";

    if($order_status_update = mysqli_query($con,$order_status))
    { 

   echo "<br><strong>Shipping Updated successfully for Order ID $order_id</strong><br>";

   }
   }

   }
}
}
}
}
}
?>

1 个答案:

答案 0 :(得分:1)

由于脚本按预期工作,直到您添加了SMS发送代码,这意味着新代码存在问题。

将SMS发送代码自己解压缩到一个文件中,并可能从命令行进行测试。不要忘记使用curl_error()curl_errno()检查cURL特定的错误(网络错误,SSL错误等),如下所示:

$data = curl_exec($ch);
if (curl_errno($ch) > 0) {
    print 'There was a cURL error: ' . $curl_error($ch);
}

还检查PHP和服务器日志以获取错误消息。