簡體   English   中英

如何將 curl arguments 翻譯成 php-curl?

[英]How to translate curl arguments to php-curl?

(我已經多次看過這個問題的某些版本,希望能用一個全面的答案列表來創建一個線程)

例如,什么是 php-curl 翻譯:

curl -v https://api-m.sandbox.paypal.com/v1/oauth2/token \
  -H "Accept: application/json" \
  -H "Accept-Language: en_US" \
  -u "client_id:secret" \
  -d "grant_type=client_credentials"

-v轉換為

curl_setopt($ch,CURLOPT_VERBOSE, 1);

PS,默認情況下,curl 將此數據發送到 stderr,從終端運行 curl 時通常可以看到 stderr,但是在 web 服務器 ala nginx/apache 后面運行 php-curl 時,stderr 鏈接到 *web- 的情況並不少見服務器的錯誤日志*,因此 VERBOSE 日志可能會到達服務器錯誤日志中。 而不是瀏覽器,對此的快速修復是設置自定義 CURLOPT_STDERR: ala:

$php_output_handle = fopen("php://output", "wb");
curl_setopt_array($ch, array(
    CURLOPT_VERBOSE => 1,
    CURLOPT_STDERR => $php_output_handle
));

但是由於 php 垃圾收集,使用此快速修復時,請記住,如果 php garabge 收集器在最后一次 curl_exec() 調用同一句柄之前關閉 $php_output_handle,它將中斷。這通常不是問題,但它可能會發生。

.. 繼續,

https://api-m.sandbox.paypal.com/v1/oauth2/token轉換為:

curl_setopt($ch,CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v1/oauth2/token');

-H "Accept: application/json" \
-H "Accept-Language: en_US" \

翻譯成

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Accept: application/json",
    "Accept-Language: en_US"
));

-u "client_id:secret"轉換為:

curl_setopt($ch,CURLOPT_USERPWD, "client_id:secret");

-d "grant_type=client_credentials" (又名--data )轉換為:

curl_setopt_array($ch, array(
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => http_build_query(array(
        "grant_type" => "client_credentials"
    ))
));

因此完整的翻譯是:

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_VERBOSE => 1,
    CURLOPT_URL => 'https://api-m.sandbox.paypal.com/v1/oauth2/token',
    CURLOPT_HTTPHEADER => array(
        "Accept: application/json",
        "Accept-Language: en_US"
    ),
    CURLOPT_USERPWD => 'client_id:secret',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => http_build_query(array(
        "grant_type" => "client_credentials"
    ))
));
curl_exec($ch);

curl -F grant_type=client_credentials的翻譯是什么? 它的:

curl_setopt_array($ch, array(
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        "grant_type" => "client_credentials"
    )
));

上傳文件呢, curl -F file=@file/path/to/upload.ext的翻譯是什么? 它的:

curl_setopt_array($ch, array(
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        "file" => new CURLFile("filepath/to/upload.ext")
    )
));

--location的翻譯是什么? 它是

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

如何上傳 JSON? 像這樣:

curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER => array(
        "Content-Type: application/json"
    ),
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => json_encode(array(
        "whatever_key" => "whatever_data"
    ))
));

-X PUT轉換為

curl_setopt($ch,CURLOPT_PUT,1);

至於--upload-file ,有幾種方法可以做到這一點,如果您正在處理容易放入 ram 的小文件,最簡單的方法是:

curl_setopt_array($ch, array(
    CURLOPT_PUT => 1,
    CURLOPT_POSTFIELDS => file_get_contents($file)
));

但如果您需要支持不想放入 RAM 的大文件,

$file = "file.ext";
$file_handle = fopen($file,"rb");
$file_size = filesize($file);
curl_setopt_array($ch, array(
    CURLOPT_UPLOAD => 1,
    CURLOPT_INFILESIZE=>$file_size,
    CURLOPT_INFILE=>$file_handle
));

暫無
暫無

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

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