比如,什么是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
));