Home » Php » Convert command line cURL to PHP cURL

Convert command line cURL to PHP cURL

Posted by: admin April 23, 2020 Leave a comment

Questions:

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!

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.

I would need to incorporate this curl command in a PHP script I’m building.

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 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 must be a variable such as $filename

http://hostname/@api/deki/pages/=TestPage/files/= must be a variable such as $pageurl

Thanks for any help.

How to&Answers:

a 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

Answer:

You need …

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

“Instantly convert curl commands to PHP code”

Answer:

Whicvhever cURL you have in command line, you can convert it to PHP with this tool:

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

It helped me after long long hours of searching for a solution! Hope it will help you out too! Your solution is this:

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

Answer:

Try this:

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

Answer:

Unfortunately SO still doesn’t have CommonMark table markup. This is an autogenerated list of which curl commandline options might map onto which php CURLOPT_ constant:

Note that this only lists somewhat exact matches of –long options to similarly named CURLOPT_ constants. But it should give you enough hints on how to compare the curl --help output and the PHP curl_setopt() list.

Answer:

the –libcurl option was added for this purpose, even though it makes a C program I think it should be fairly easy to translate to PHP

Answer:

Using MYYN’s answer as a starting point, and this page as a reference on how to send POST data using PHP cURL, here is my suggestion (I am working on something very similar at the moment):

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

You can probably optimize the many curl_setopts with the use of a curl_setopt_array() call if you desire.

Answer:

Better this. In one line.

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