php curl 命令,将命令行cURL转换为PHP cURL

将命令行cURL转换为PHP cURL

我从来没有做过任何curl,所以需要一些帮助。 我试图从例子中解决这个问题,但是却无法摆脱困境!

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

我需要将这个curl命令join到我正在构build的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包含authentication,但我没有问题在脚本中明确的文字,因为这将只由我运行。

@ test.png必须是一个variables,如$ filename

http:// hostname / @ api / deki / pages / = TestPage / files / =必须是一个variables,如$ pageurl

感谢您的帮助。

起点:

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

尝试这个:

$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);

–libcurl选项是为此目的添加的,即使它使一个C程序,我认为它应该是相当容易的翻译成PHP

以MYYN的答案为出发点,并将此页面作为如何使用PHP cURL发送POST数据的参考,这里是我的build议(我目前正在处理类似的事情):

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

如果你愿意的话,你可以使用curl_setopt_array()调用来优化许多curl_setopts。

这更好。 在一行中。

$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);

你需要 …

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

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

无论你在命令行中使用了哪些cURL,都可以使用这个工具将其转换为PHP:

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

它帮助我长时间寻找解决scheme! 希望它能帮助你! 你的解决scheme是这样的

// 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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值