search

Home  >  Q&A  >  body text

Convert command line cURL to PHP cURL

<p>I have never done any curling before so need some help. I tried to solve this problem from examples but can't understand it! </p> <p>I have a curl command that I can successfully run from the linux (ubuntu) command line to put files into the wiki via the api. </p> <p>I need to incorporate this curl command into a PHP script I'm building. </p> <p>How do I translate this curl command so that it works in a PHP script? </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>The cookie.txt contains the authentication, but I have no problem putting it in clear text into the script since this is only run by me. </p> <p>@test.png must be a variable, such as $filename</p> <p>http://hostname/@api/deck/pages/=TestPage/files/= must be a variable, such as $pageurl</p> <p>Thanks for your help. </p>
P粉323224129P粉323224129459 days ago509

reply all(2)I'll reply

  • P粉030479054

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

    you need to...

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

    "Instantly convert curl commands to PHP code"

    reply
    0
  • P粉022285768

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

    starting point:

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

    See also: http://www.php.net/manual /en/function.curl-setopt.php

    reply
    0
  • Cancelreply