簡體   English   中英

curl的等效php選項

[英]Equivalent php options for curl

我正在嘗試使用以下選項發送curl請求,但我不知道如何在php curl設置中使用-d選項發送數據。

curl -X 'POST' \
     -H 'Content-Type: application/json; charset=utf-8' \
     -H 'Authorization: Bearer x'
     -v 'URL' \
     -d
      '{
         "input": {
           "urn": "num",
           "compressedUrn": true,
           "rootFilename": "A5.iam"
         }
       }'

換句話說,我知道如何使用...發送標題

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Authorization: Bearer x'
    ));

但我不知道-d標志的等價物。

謝謝

它是需要隨請求一起發送的數據

我通常將其包裝成一個函數,以使處理錯誤/成功更容易。 特別是如果你正在處理像paypal或類似的API

// create the object (you can do this via a string if you want just remove the json encode from the postfields )

$request = new stdClass(); //create a new object
$request->input = new stdClass(); // create input object
$request->input->urn = 'num'; // assign values
$request->input->compressedUrn = true;
$request->input->rootFilename = 'A5.iam';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'URL HERE');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $request ) ); // encode the object to be sent
curl_setopt($ch, CURLOPT_POST, true); // set post to true        
curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [  //set headers
    'Content-Type: application/json',
    'Authorization: Bearer x'
]);
$result = curl_exec ($ch);
if ( ! $result) { //check if the cURL was successful.
    // do something else if cURL fails
}

curl_close ($ch);

$return = json_decode( $result ); // object if expecting json return

但我不知道-d標志的等價物。

這是CURLOPT_POSTFIELDS。

curl_setopt_array($ch, array(
    CURLOPT_URL => 'URL',
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => array(
        'Authorization: Bearer x',
        'Content-Type: application/json; charset=utf-8'
    ),
    CURLOPT_POSTFIELDS => json_encode(array(
        'input' => array(
            'urn' => 'num',
            'compressedUrn' => true,
            'rootFilename' => 'A5.iam'
        )
    ))
));

暫無
暫無

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

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