PHP 卷曲发送短信
PHP Curl to send SMS
对于 PHP 和 cURL,我几乎处于初学者水平。
该问题与 php CronJob 脚本 有关,该脚本从 phpmyadmin 数据库获取与具有特定状态的客户订单相关的数据(比如“0”,表示短信不是 sent/or 未向客户发送更新)并更新运输详细信息。
在添加代码以通过 API 从我的 SMS 服务提供商发送 SMS 之前,脚本运行良好,用于发送电子邮件,关于运送更新,然后更新数据库,如订单状态和其他必要的详细信息给客户以及系统。
The issue arises that if, suppose 100-150 results are fetched as
arrays with status='0' the loop only executes once and stops, whereas
before adding the SMS code, it used to run for n number rows
fetched with status='0'.
我已经尝试了 Whosebug、PHP 手册、W3C 以及开发人员列出的几乎所有方法,但我仍然无法做到成功获取所有结果。
我不想做的事 -
1。 Select 来自 table 的数据,其中 status='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'> </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> </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 />
</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> </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. </span></p>
<p> </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>";
}
}
}
}
}
}
}
}
?>
由于在您添加短信发送代码之前脚本按预期工作,这意味着新代码存在问题。
将 SMS 发送代码单独提取到一个文件中并进行测试,也许可以从命令行进行。不要忘记使用 curl_error()
and curl_errno()
检查特定于 cURL 的错误(网络错误、SSL 错误等),如下所示:
$data = curl_exec($ch);
if (curl_errno($ch) > 0) {
print 'There was a cURL error: ' . $curl_error($ch);
}
检查 PHP 和服务器日志是否有错误消息。
对于 PHP 和 cURL,我几乎处于初学者水平。 该问题与 php CronJob 脚本 有关,该脚本从 phpmyadmin 数据库获取与具有特定状态的客户订单相关的数据(比如“0”,表示短信不是 sent/or 未向客户发送更新)并更新运输详细信息。
在添加代码以通过 API 从我的 SMS 服务提供商发送 SMS 之前,脚本运行良好,用于发送电子邮件,关于运送更新,然后更新数据库,如订单状态和其他必要的详细信息给客户以及系统。
The issue arises that if, suppose 100-150 results are fetched as arrays with status='0' the loop only executes once and stops, whereas before adding the SMS code, it used to run for n number rows fetched with status='0'.
我已经尝试了 Whosebug、PHP 手册、W3C 以及开发人员列出的几乎所有方法,但我仍然无法做到成功获取所有结果。
我不想做的事 - 1。 Select 来自 table 的数据,其中 status='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'> </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> </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 />
</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> </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. </span></p>
<p> </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>";
}
}
}
}
}
}
}
}
?>
由于在您添加短信发送代码之前脚本按预期工作,这意味着新代码存在问题。
将 SMS 发送代码单独提取到一个文件中并进行测试,也许可以从命令行进行。不要忘记使用 curl_error()
and curl_errno()
检查特定于 cURL 的错误(网络错误、SSL 错误等),如下所示:
$data = curl_exec($ch);
if (curl_errno($ch) > 0) {
print 'There was a cURL error: ' . $curl_error($ch);
}
检查 PHP 和服务器日志是否有错误消息。