curl 同步代码

<?php
// Done for EUE

require_once __DIR__ . '/../../../functions/_email_send.php';

// Compatibility with Mobile 2
if (!isset($issue_code) && isset($issue_id)) {
    $issue_code = $issue_id;
}

$logfile = fopen(
    __DIR__ . '/logs/_sp_issue_valid_' . date('Y-m-d') . '.txt',
    'a'
);
fwrite($logfile, 'issue_code传参为:' . $issue_code . "\n");
fclose($logfile);

$query = $DB->prepare("
    SELECT distinct
        issue_code,
        issue_qty,
        sp_code,
        left(location_code,1) as sp_ff_4,
        employee_code AS requester,
        mic_cost_center.cost_center_code as 'cost_center',
        left(asset_ff_5,10) as sp_ff_6,
        issue_destination

    FROM sp_issue_details
    INNER JOIN sp_issue ON sp_issue.issue_id = sp_issue_details.issue_id AND  sp_issue.issue_creator_id
    inner join (select wo_id,wo_asset_id from wo_list union all select wo_id,asset_id from wo_history) b on sp_issue.wo_id=b.wo_id
    INNER JOIN sp_list ON sp_issue_details.sp_id = sp_list.sp_id

    inner join asset_location on sp_issue.issue_location_id=asset_location.location_id
    inner JOIN admin_employee ON admin_employee.employee_id = sp_issue.employee_id
    inner join asset_list on b.wo_asset_id=asset_list.asset_id
    inner JOIN mic_cost_center ON mic_cost_center.cost_center_id = asset_list.asset_cost_center_id
    WHERE sp_issue.issue_code =:issue_code
;");
$query->bindValue(':issue_code', $issue_code);
$query->execute();

$details = [];
$line = 0;
while ($row = $query->fetch(PDO::FETCH_OBJ)) {
    $line++;

    $werks = '';
    if ($row->sp_ff_4 == 'B') {
        $werks = '1756';
    } elseif ($row->sp_ff_4 == 'C') {
        $werks = '2756';
    }

    $details[] = [
        'num' => $line,
        'issue_code' => $row->issue_code,
        'issue_qty' => $row->issue_qty,
        'sp_code' => $row->sp_code,
        'requester' => $row->requester,
        'werks' => $werks,
        'cost_center' => $row->cost_center,
        'sp_ff_6' => $row->sp_ff_6,
        'issue_destination'=>$row->issue_destination,
    ];
}

$data = ['data' => $details];
$json = json_encode($data);

$logfile = fopen(
    __DIR__ . '/logs/_sp_issue_valid_' . date('Y-m-d') . '.txt',
    'a'
);
fwrite($logfile, "请求传参: {$json}\n");
fclose($logfile);

$curl = curl_init('http://sapprd01.eue.com:8001/sap/zeamic/erp/zckid');

curl_setopt($curl, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Content-Length: ' . strlen($json),
]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
$fp = fopen(dirname(__FILE__) . '/_sp_issue_valid.txt', 'w');
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_STDERR, $fp);

$response = curl_exec($curl);
$res = json_decode($response,true);
if(!empty($res['e_succ']) && $res['e_succ'] == 'S'){
    $logfile = fopen(
        __DIR__ . '/logs/_sp_issue_valid_' . date('Y-m-d') . '.txt',
        'a'
    );

    fwrite($logfile, '返回结果: ' . $response . "\n");
    fclose($logfile);
}else{
    $logfile = fopen(
        __DIR__ . '/logs/_sp_issue_valid_' . date('Y-m-d') . '.txt',
        'a'
    );
    fwrite($logfile, 'sap错误信息: ' . curl_error($curl) . "\n");
    fclose($logfile);

    $msg = "出库单号:".$issue_code.",同步出现错误:".curl_error($curl).",请知悉,谢谢";
    send_email('GXLI@EUEBattery.com', '出库同步出现错误', $msg, $msg);
}

 

posted @ 2022-10-18 10:40  王越666  阅读(32)  评论(0编辑  收藏  举报