将命令行 cURL 转换为 PHP cURL卷曲到PHP:https://incarnate.github.io/curl-to-php/

2022-08-30 12:51:30

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

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

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

如何翻译此 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包含身份验证,但我在脚本中以明文形式放置此内容没有问题,因为这将仅由我运行。

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

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

感谢您的任何帮助。


答案 1

一个起点:

<?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

...
?>

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


答案 2

你需要。。。

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

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


推荐