将命令行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)命令
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
感谢您的帮助。
推荐答案
b
<?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屋!