Skip to content
Advertisement

Convert command line cURL to PHP cURL

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.

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement