【问题标题】:How to translate curl arguments to php-curl?如何将 curl 参数转换为 php-curl?
【发布时间】:2021-03-09 13:39:11
【问题描述】:

(我已经多次看到这个问题的 SOME 版本,希望能创建一个包含综合答案列表的线程)

【问题讨论】:

  • 这个问题不允许确定一个正确的答案......并且错误处理是有问题的。如果你想建立一个规范的答案,这取决于元。
  • @MartinZeitler 好吧,我想在某个地方指出问题,例如 thisthisthisthis 下次我看到它们时;
  • @MartinZeitler 至于错误处理,您正在考虑类似function ex_curl_setopt($ch,int $option, $val){if(!curl_setopt($ch,$option,$val)){throw new \RuntimeException("curl_setopt failed: ".curl_errno($ch).":".curl_error($ch));}} 对吗?

标签: php curl php-curl


【解决方案1】:

比如,什么是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,但是当在 webserver ala nginx/apache 后面运行 php-curl 时,stderr 链接到 *web-server 的错误日志并不少见*,因此 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 垃圾收集,在使用这个 quickfix 时,请记住,如果 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
));

【讨论】:

    猜你喜欢
    • 2019-09-01
    • 2017-06-05
    • 2020-05-29
    • 2017-08-03
    • 2019-06-04
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    相关资源
    最近更新 更多