18910140161

PHP curl 请求之后保存数据库中

顺晟科技

2022-09-15 12:27:58

112


    $headers[]="ClientID:JJZ。。。";
    $headers[]="ServiceName:NEZ.。。。。";



    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, 'http://192.168.99.199/api/exchange');
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER,0);
    //设置请求头
    curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
    //设置获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    //设置post数据
    $post_data = '{"StartTime": "2021-07-13 00:00:00","EndTime": "2021-07-13 23:59:00"}';
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    //执行命令
    $result = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);


    $con = mysqli_connect("localhost","root","root","diandong");

$result = mysqli_query($con,"SELECT * FROM diandong");

    $sql = $con->query('INSERT INTO test values ()');

请求之后如何保存到mysql数据库中

主要是要data里面的 soid pirname shift之类的存到数据库中

数据格式
{ResulType: 0, ErrorCode: 0, ErrorMessage: "", DataRecordCount: 0, Data: [,…], Title: null,…}
CostMilliseconds: 0
Data: [,…]
0: {SoiID: 21986, PirName: "江西", Shift: "202107130", VesselChn: "民亚99", Voyage: "Z2157W", IEFlag: "出口"

大概是这样吧

<?php
$result = curl_exec($curl);
$json = json_decode($result, true);
$con = mysqli_connect("localhost","root","root","diandong");
$stmt = mysqli_prepare($con, "insert into test (SoiID, PirName, Shift, VesselChn, Voyage, IEFlag) values (?,?,?,?,?,?)");
mysqli_stmt_bind_param($stmt, "ssssss", $SoiID, $PirName, $Shift, $VesselChn, $Voyage, $IEFlag);
if(!empty($json['Data'])) {
    foreach($json['Data'] as $v) {
        extract($v);
        mysqli_stmt_execute($stmt);
    }
}

没有环境, 只能这样写一下....

相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航