将命令行 cURL 转换为 PHP cURL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1939609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:22:29  来源:igfitidea点击:

Convert command line cURL to PHP cURL

phpcurl

提问by Brigante

I've never done any curl before so am in need of some help. I've tried to work this out from examples but cannot get my head around it!

我以前从未做过任何卷曲,所以需要一些帮助。我试图从示例中解决这个问题,但无法理解它!

I have a curl command that I can successfully run from a linux(ubuntu) command line that puts a file to a wiki through an api.

我有一个 curl 命令,我可以从 linux(ubuntu) 命令行成功运行该命令,该命令行通过 api 将文件放入 wiki。

I would need to incorporate this curl command in a PHP script I'm building.

我需要将此 curl 命令合并到我正在构建的 PHP 脚本中。

How can I translate this curl command so that it works in a PHP script?

如何翻译此 curl 命令,使其在 PHP 脚本中工作?

curl -b cookie.txt -X PUT \
     --data-binary "@test.png" \
     -H "Content-Type: image/png" \    
     "http://hostname/@api/deki/pages/=TestPage/files/=test.png" \
     -0

cookie.txt contains the authentication but I don't have a problem putting this in clear text in the script as this will be run by me only.

cookie.txt 包含身份验证,但我没有问题将它以明文形式放入脚本中,因为这只会由我运行。

@test.png must be a variable such as $filename

@test.png 必须是一个变量,例如 $filename

http://hostname/@api/deki/pages/=TestPage/files/=must be a variable such as $pageurl

http://hostname/@api/deki/pages/=TestPage/files/=必须是 $pageurl 等变量

Thanks for any help.

谢谢你的帮助。

采纳答案by miku

a starting point:

一个起点:

<?php

$pageurl = "http://hostname/@api/deki/pages/=TestPage/files/=";
$filename = "test.png";

$theurl = $pageurl . $filename;

$ch = curl_init($theurl);
curl_setopt($ch, CURLOPT_COOKIE, ...); // -b
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // -X
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: image/png']); // -H
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); // -0

...
?>

See also: http://www.php.net/manual/en/function.curl-setopt.php

另见:http: //www.php.net/manual/en/function.curl-setopt.php

回答by kris

You need ...

你需要 ...

curl-to-PHP : https://incarnate.github.io/curl-to-php/

curl-to-PHP : https://incarnate.github.io/curl-to-php/

"Instantly convert curl commands to PHP code"

“立即将 curl 命令转换为 PHP 代码”

回答by Vladimir Despotovic

Whicvhever cURL you have in command line, you can convert it to PHP with this tool:

无论您在命令行中有什么 cURL,都可以使用此工具将其转换为 PHP:

https://incarnate.github.io/curl-to-php/

It helped me after long long hours of searching for a solution! Hope it will help you out too! Your solution is this:

经过长时间的寻找解决方案后,它帮助了我!也希望能帮到你!你的解决方案是这样的:

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://hostname/@api/deki/pages/=TestPage/files/=test.png");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
    "file" => "@" .realpath("test.png")
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");


$headers = array();
$headers[] = "Content-Type: image/png";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

回答by manny

Try this:

尝试这个:

$cmd='curl -b cookie.txt -X PUT \
     --data-binary "@test.png" \
     -H "Content-Type: image/png" \    
     "http://hostname/@api/deki/pages/=TestPage/files/=test.png" \
     -0';
exec($cmd,$result);

回答by mario

Unfortunately SO still doesn't have CommonMark table markup. This is an autogenerated list of which curl commandline optionsmight map onto which php CURLOPT_ constant:

不幸的是,SO 仍然没有 CommonMark 表标记。这是一个自动生成的列表,其中列出了哪些curl 命令行选项可能映射到哪些 php CURLOPT_ 常量:

Note that this only lists somewhat exact matches of --long options to similarly named CURLOPT_ constants. But it should give you enough hints on how to compare the curl --helpoutput and the PHP curl_setopt() list.

请注意,这仅列出了 --long 选项与类似命名的 CURLOPT_ 常量的精确匹配。但它应该为您提供有关如何比较curl --help输出和 PHP curl_setopt() 列表的足够提示 。

回答by Daniel Stenberg

the --libcurl option was added for this purpose, even though it makes a C program I think it should be fairly easy to translate to PHP

为此添加了 --libcurl 选项,即使它是一个 C 程序,我认为它应该很容易转换为 PHP

回答by r00fus

Using MYYN's answer as a starting point, and this pageas a reference on how to send POST data using PHP cURL, here is my suggestion (I am working on something very similar at the moment):

使用 MYYN 的答案作为起点,并将此页面作为有关如何使用 PHP cURL 发送 POST 数据的参考,这是我的建议(我目前正在研究非常相似的内容):

<?php

$pageurl = "http://hostname/@api/deki/pages/=TestPage/files/=";
$filename = "test.png";

$theurl = $pageurl.$filename;

$ch = curl_init($theurl);
curl_setopt($ch, CURLOPT_COOKIE, ...); // -b
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // -X
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: image/png']); // -H
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); // -0

$post = array("$filename"=>"@$filename");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$response = curl_exec($ch);
?>

You can probably optimize the many curl_setopts with the use of a curl_setopt_array() call if you desire.

如果您愿意,您可以使用 curl_setopt_array() 调用优化许多 curl_setopts。

回答by Toxicity

Better this. In one line.

这个更好。在一行。

$cmd='curl -b cookie.txt -X PUT --data-binary "@test.png" -H "Content-Type: image/png" "http://hostname/@api/deki/pages/=TestPage/files/=test.png" -0';
exec($cmd,$result);