将命令行 cURL 转换为 PHP cURL [英] Convert command line cURL to PHP cURL
问题描述
我以前从未做过任何卷曲,所以需要一些帮助.我试图从示例中解决这个问题,但无法理解它!
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!
我有一个 curl 命令,我可以从 linux(ubuntu) 命令行成功运行该命令,该命令行通过 api 将文件放入 wiki.
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 命令合并到我正在构建的 PHP 脚本中.
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 -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 包含身份验证,但我将其以明文形式放入脚本中没有问题,因为这将仅由我运行.
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.
@test.png 必须是 $filename 等变量
@test.png must be a variable such as $filename
http://hostname/@api/deki/pages/=TestPage/files/= 必须是一个变量,例如 $pageurl
http://hostname/@api/deki/pages/=TestPage/files/= must be a variable such as $pageurl
感谢您的帮助.
推荐答案
一个起点:
<?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
这篇关于将命令行 cURL 转换为 PHP cURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!