如何将卷曲响应值插入数据库

时间:2018-11-20 12:59:16

标签: php

我在link中的以下代码的帮助下生成了AWBNoSeries:

ReturnMessage”:“成功”,“ 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“

我没有在上述查询中传递值()所需的内容。...

2 个答案:

答案 0 :(得分:1)

您将需要遍历找到的所有值,并将每个值添加到数据库中。为此,我在使用Prepared Statements方面拥有丰富的经验。使用此方法,您可以设置该语句并将其一次发送到SQL Server,然后只需更改为查询提供的参数(在本例中为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);

?>

答案 1 :(得分:0)

如何将curl响应 JSON 值插入数据库:

  <?php
$hostname = "localhost";      // That Is For Database Connection
$database = "demo";
$username = "root";
$password = "root";
$link = mysql_connect($hostname, $username, $password);
mysql_select_db($database) or die('Could not select database');
function get_data($url) {
$ch = curl_init();                     // That Is For Curl
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = "http://socialinfotech.in/development/jsondemo/category.json";  // Pass Your json Url
$data = get_data($url);
$data_json = json_decode($data, true);  // For Decord Your Json Value
foreach ($data_json['Result'] as $key => $value) {        // Foreach Because Multiple Values

mysql_query("INSERT INTO `demo`.`category` (category_id, category_name, category_image) 
   VALUES ('" . $value['category_id'] . "', '" . $value['category_name'] . "', '" . $value['category_image'] . "')");

foreach ($value['card'] as $keys => $values) {
mysql_query("INSERT INTO `demo`.`card` (ref_category_id,  card_image, user_name, user_image) 
   VALUES ('" . $value['category_id'] . "','" . $values['card_image'] . "', '" .$values['user_name']. "', '" .$values['user_image']. "')");
}
}
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<h1>Check Your Database Value</h1>
</body>

那是我的杰森价值观 Json Value

那是我在数据库中的插入值 Insert Value In Database