【问题标题】:$body->append(); from POSTMAN API, how to replace it with variables?$body->append();来自 POSTMAN API,如何用变量替换它?
【发布时间】:2021-01-20 04:20:26
【问题描述】:

鉴于这个从邮递员生成的代码,$body->append();来自POSTMAN API,如何用变量替换它?

如何将 CURLOPT_POSTFIELDS 替换为 php 变量?所以可以用动态变量填充

最好的做法是什么?

谢谢

<?php

$json = '{
    "RateRequest": {
        "ClientDetails" : null,
        "RequestedShipment": {
            "DropOffType": "REQUEST_COURIER",
            "ShipTimestamp": "2020-08-25T11:00:00GMT+02:00",
            "UnitOfMeasurement": "SI",
            "Content": "NON_DOCUMENTS",
            "PaymentInfo": "DAP",
            "NextBusinessDay": "Y",
            "Account": "54xxxxxx",
            "Ship": {
                "Shipper": {
                    "City": "city",
                    "PostalCode": 123456,
                    "CountryCode": "ID"
                },
                "Recipient" : {
                "City" : "canberra",
                "PostalCode" : 2601,
                "CountryCode" : "AU"
                }
            },
            "Packages": {
                "RequestedPackages": {
                    "@number": "1",
                    "Weight": {
                        "Value": 8.82
                    },
                    "Dimensions": {
                        "Length": 4.33,
                        "Width": 4.33,
                        "Height": 4.33
                    }
                }
            }
        }
    }
}';

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://wsbexpress.dhl.com/rest/sndpt/RateRequest",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $json,
  CURLOPT_HTTPHEADER => array(
    "Authorization: Basic YmF0dWxheWFuZ3NJRDpKXjBlUEA2dkQhM2Y=",
    "Content-Type: application/json",
    "Cookie: BIGipServer~WSB~pl_wsb-express-cbj.dhl.com_443=293349575.64288.0000"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

【问题讨论】:

    标签: php json api curl postman


    【解决方案1】:
    • json_decode 字符串成数组
    • 根据需要修改值
    • 为 CURLOPT_POSTFIELDS 设置该数据

    类似的东西

    $json = '{
        "RateRequest": {
            "ClientDetails" : null,
            "RequestedShipment": {
                "DropOffType": "REQUEST_COURIER",
                "ShipTimestamp": "2020-08-25T11:00:00GMT+02:00",
                "UnitOfMeasurement": "SI",
                "Content": "NON_DOCUMENTS",
                "PaymentInfo": "DAP",
                "NextBusinessDay": "Y",
                "Account": "54xxxxxx",
                "Ship": {
                    "Shipper": {
                        "City": "city",
                        "PostalCode": 123456,
                        "CountryCode": "ID"
                    },
                    "Recipient" : {
                    "City" : "canberra",
                    "PostalCode" : 2601,
                    "CountryCode" : "AU"
                    }
                },
                "Packages": {
                    "RequestedPackages": {
                        "@number": "1",
                        "Weight": {
                            "Value": 8.82
                        },
                        "Dimensions": {
                            "Length": 4.33,
                            "Width": 4.33,
                            "Height": 4.33
                        }
                    }
                }
            }
        }
    }';
    
    $data = json_decode($json, true);
    
    if (isset($data['RateRequest']['RequestedShipment']['DropOffType'])) {
    
       $data['RateRequest']['RequestedShipment']['DropOffType'] = 'some new value';
    
    }
    
    
    //... later in your curl call...
    
     CURLOPT_POSTFIELDS => $data,
    

    【讨论】:

    • 哦,天哪,为什么我没有想到.. 非常感谢 Wesley Smith
    猜你喜欢
    • 2023-03-22
    • 2018-04-10
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    相关资源
    最近更新 更多