首页  >  问答  >  正文

将命令行 cURL 转换为 PHP cURL

<p>我以前从未做过任何卷曲,所以需要一些帮助。我试图从示例中解决这个问题,但无法理解它!</p> <p>我有一个curl命令,我可以从linux(ubuntu)命令行成功运行它,通过api将文件放入wiki。</p> <p>我需要将此curl 命令合并到我正在构建的PHP 脚本中。</p> <p>我如何翻译这个curl命令,以便它在PHP脚本中工作?</p> <pre class="brush:php;toolbar:false;">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</pre> <p>cookie.txt 包含身份验证,但我将其以明文形式放入脚本中没有问题,因为这仅由我运行。</p> <p>@test.png 必须是变量,例如 $filename</p> <p>http://hostname/@api/deck/pages/=TestPage/files/= 必须是变量,例如 $pageurl</p> <p>感谢您的帮助。</p>
P粉323224129P粉323224129394 天前421

全部回复(2)我来回复

  • P粉030479054

    P粉0304790542023-08-25 18:03:55

    你需要...

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

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

    回复
    0
  • P粉022285768

    P粉0222857682023-08-25 12:35:36

    起点:

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

    回复
    0
  • 取消回复