search

Home  >  Q&A  >  body text

Convert cURL command line to PHP cURL code

<p>I have never used curl before so need some help. I tried to figure it out from examples but couldn't understand! </p> <p>I have a curl command that I can successfully run from the Linux (Ubuntu) command line to upload files to a wiki via the API. </p> <p>I need to incorporate this curl command into a PHP script I'm building. </p> <p>How can I convert this curl command into a form that is valid 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 information, but I have no problem putting it in clear text in the script since this will only be run by me. </p> <p>@test.png must be a variable, such as $filename</p> <p>http://hostname/@api/deki/pages/=TestPage/files/= must be a variable, such as $pageurl</p> <p>Thanks for any help. </p>
P粉018653751P粉018653751595 days ago733

reply all(2)I'll reply

  • P粉459440991

    P粉4594409912023-08-15 09:27:43

    you need to ...

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

    "Convert curl command to PHP code immediately"

    reply
    0
  • P粉258083432

    P粉2580834322023-08-15 00:18:05

    A starting point:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

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

     

    ...

    ?>

    You can also refer to: http://www.php.net/manual/en/function.curl-setopt.php

    reply
    0
  • Cancelreply