簡體   English   中英

如何在 php 數組 json 中使用 curl 發送發布數據

[英]how to send post data using curl in php array json

如何在 php 數組 json 中使用 curl 發送發布數據

像數組一樣發布 curl 參數訂單列表這是我的代碼

<?php
class PostOrderPolicy extends CI_Controller{
    public function index(){
        $data = $this->getData();
        foreach ($data as $key) {
            $orderListt = array(
                    'orderDate' => $key['order_date'],
                    'IDNumber' => $key['IDnumber'],
                    'itemDescription' => $key['description']
                    );

            $send = $this->sendData($orderListt, $key['resi_number']);
            if ($send) {
                $res["message"] = "Sukses";
            }else{
                $res["message"] = "Gagal";
            }
        }
       
        //echo json_encode($res);
    }

    private function getData(){
        $sql = "SELECT * FROM t_transaction WHERE status='00'";
        return $this->db->query($sql)->result_array();
    }

    private function sendData($data){
        $result = array();
        $url = "https://contoh.co.id/createPolicy";

        $res    = array();
        $sign   = $this->getSign();
        $upperSign = strtoupper($sign);
        //$random = $this->getRandomStr();
        $random = '049KN1PSOL16QHIF';
        $batch  = 'BATCH000002';

        $dataArr = array(
            'sign' => $upperSign,
            'randomStr' => $random,
            'batchNo' => $batch,
            'orderList' => [$data]
        );

        $string     = json_encode($dataArr);
        $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

        $response = json_decode(curl_exec($ch), TRUE);
        print_r($response);
        print_r($string);
    }
}
?>

但我的結果帖子字段 [在此處輸入圖像描述][1] [1]:https://i.stack.imgur.com/b7LEP.png

我想要這樣 [在此處輸入圖像描述][2] [2]:https://i.stack.imgur.com/xExJ2.png

{"sign":"35EAF78F95A52DDA79FC9911559026F6","randomStr":"049KN1PSOL16QHIF","batchNo":"BATCH000002","orderList":[{"orderDate":"2020-07-01 11:00:00.000","resiNumber":"99875646957","itemDescription":"testing"},{"orderDate":"2020-07-01 ]11:00:00.000","IDNumber":"28929740170","itemDescription":"testing"}]}

返回響應而不是輸出:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

執行 POST 請求:

$result = curl_exec($ch);

關閉 cURL 資源:

curl_close($ch);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM