php - 如何将 curl 响应值插入数据库
问题描述
我借助链接中的以下代码生成了AWBNoSeries:
ReturnMessage":"Successful","ReturnCode":100,"AWBNoGenRequestedDateTime":"20-11-2018 11:46:35","BatchID":"UQpyj61049","AWBNoSeries":["14104918100000","14104918100001" ,
<?php
$data =
array (
'BusinessUnit' => 'ECOM',
'ServiceType' => 'FORWARD',
'BatchID' => 'UQpyj61049',
);
$url = "http://114.143.206.69:803/StandardForwardStagingService.svc/GetAWBNumberGeneratedSeries";
$data = json_encode($data);
$headers = array(
"Content-Type: application/json",
"XBKey: QGfMthH1",
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$curl_response = curl_exec($curl);
curl_close($curl);
echo $curl_response;
$con = mysqli_connect("localhost","root","","do_management4");
$result = mysqli_query($con,"SELECT * FROM ecomexpress_awb");
//$id = $_POST['id'];
$sql = $con->query('INSERT INTO ecomexpress_awb(awb) values ()');
mysqli_close($con);
?>
现在我需要将这些保存在 AWBNoSeries 下面的 mysql 表ecomexpress_awb和列awb中,每个 AWBNo 在不同的行中......
14104918100000","14104918100001","14104918100002","14104918100003"
我没有得到我需要在上面的查询中传递值()的东西......
解决方案
您将需要遍历找到的所有值并将每个值添加到数据库中。为此,我在使用Prepared Statements方面有很好的经验。使用此方法,您可以设置语句并将其发送到 SQL 服务器一次,然后只需更改您提供给查询的参数(在本例中为 AWBNo 和 AWBNoGenRequestedDateTime),并为每个值执行。完整的代码如下所示:
<?php
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(-1);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$data =
array (
'BusinessUnit' => 'ECOM',
'ServiceType' => 'FORWARD',
'BatchID' => 'UQpyj61049',
);
$url = "http://114.143.206.69:803/StandardForwardStagingService.svc/GetAWBNumberGeneratedSeries";
$data = json_encode($data);
$headers = array(
"Content-Type: application/json",
"XBKey: QGfMthH1",
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$curl_response = curl_exec($curl);
curl_close($curl);
//we know this is working, no need to echo data
//echo $curl_response;
// justin code start
$mysqli = new mysqli("localhost", "root", "your_password", "do_management4");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$parsedData = json_decode($curl_response, true); //true: preserve associative arrays
if (!($stmt = $mysqli->prepare("INSERT INTO ecomexpress_awb(awb, created_at) VALUES (?,?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
//bind parameters: $awb will be the AWBNoSeries values, $genRequestDT will be the AWBNoGenRequestedDateTime (same for all AWBNoSeries values)
if (!$stmt->bind_param("is", $awb, $genRequestDT)) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
//set $genRequestDT equal to AWBNoGenRequestedDateTime value to be inserted with each record (this only needs to be done once)
$genRequestDT = $parsedData['AWBNoGenRequestedDateTime'];
//loop through AWBNoSeries values and insert each one into the db
foreach ($parsedData['AWBNoSeries'] as $awb){
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
}
$stmt->close(); //close the statement
// justin code end
mysqli_close($mysqli);
?>
推荐阅读
- python - 控制台网格未正确打印
- python - 努力创建一个会话,以便我可以登录到一个站点并通过 Python 转到我需要的页面
- c# - Blazor - 绑定到服务的属性,由另一个组件更改
- c++ - 使用 lower/upper_bound() 在向量中查找最接近的值
- javascript - indexOf 方法过滤器在 React 中创建了意外的输出
- matplotlib - mac os x 上 anaconda 上的 matplotlib
- c++ - 在 Winsock ws 客户端和 Websocket 服务器之间建立握手
- javascript - 如何将函数存储在具有全局范围的 Javascript 对象中
- c - 在C中的for循环中使用分号而不使用任何命令
- intellij-idea - IntelliJ Play2 设置未保存