Convert Command Line Curl to PHP Curl

Convert curl command to PHP cURL - kaaProject

I would suggest you to try the following code:

    $notification = array("applicationId" =>"32768",
"schemaId"=>"32771",
"topicId"=>"32768",
"type"=>"USER"
);

$ch = curl_init();

$headers = array();
$headers[] = 'Content-Type: multipart/form-data';

curl_setopt($ch, CURLOPT_USERPWD, "devuser:devuser123");
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/kaaAdmin/rest/api/sendNotification');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'notification' => json_encode($notification),
'file' => '@' . 'notification.json'
));
$content = curl_exec($ch);
print $content;
curl_close($ch);

Basically this code the same thing as bash command you provided, but with one difference - it does not provide Content-Type for notification param.

Instead of

--------------------------5766b31cc6648aa7
Content-Disposition: form-data; name="notification"
Content-Type: application/json

{"applicationId":"32768","schemaId":"32771","topicId":"32768","type":"USER"}
--------------------------5766b31cc6648aa7

it sends

--------------------------dd3a987c4561b96a
Content-Disposition: form-data; name="notification"

{"applicationId":"32768","schemaId":"32771","topicId":"32768","type":"USER"}
--------------------------dd3a987c4561b96a

However, I think this still should work fine. If not, please get back to me and I will provide you a different script that won't be that nice and clear, but will work.

================ UPDATE 1 ====================

Here's ugly script that must work

function multipart_build_query($fields, $boundary){
$retval = '';
foreach($fields as $name => $v){
$data = $v[0];
$filename = (isset($v[1])) ? '; filename="' . $v[1] . '"' : '';
$type = (isset($v[2])) ? 'Content-Type: ' . $v[2] . "\n" : '';

$retval .= "--$boundary\n" .
"Content-Disposition: form-data; name=\"$name\"$filename\n$type\n" .
"$data\n";
}
$retval .= "--$boundary--\n";
return $retval;
}


$notification = array("applicationId" =>"32768",
"schemaId"=>"32771",
"topicId"=>"32768",
"type"=>"USER");

$post_data = array(
'notification' => array(
json_encode($notification),
null,
'application/json'
),
'file' => array(
file_get_contents('notification.json'),
'notification.json',
'application/octet-stream'
)
);

$boundary = md5(time() . mt_rand(1, 65536));


$ch = curl_init();
$headers = array();
$headers[] = 'Content-Type: multipart/form-data; boundary=' . $boundary;
curl_setopt($ch, CURLOPT_USERPWD, "devuser:devuser123");
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/kaaAdmin/rest/api/sendNotification');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, multipart_build_query($post_data, $boundary));
$content = curl_exec($ch);
print $content;
curl_close($ch);

----- UPDATE 2: Raw request added -----

POST /kaaAdmin/rest/api/sendNotification HTTP/1.1
Host: localhost:8088
Authorization: Basic ZGV2dXNlcjpkZXZ1c2VyMTIz
User-Agent: curl/7.43.0
Accept: */*
Content-Length: 430
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------f03cb5aef819a622

--------------------------f03cb5aef819a622
Content-Disposition: form-data; name="notification"
Content-Type: application/json

{"applicationId":"32768","schemaId":"32771","topicId":"32768","type":"USER"}
--------------------------f03cb5aef819a622
Content-Disposition: form-data; name="file"; filename="notification.json"
Content-Type: application/octet-stream

asdasdasdad

--------------------------f03cb5aef819a622--

Convert command line cURL to PHP cURL

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

Convert Terminal cURL to PHP cURL Script

It worked after adding this line to the cURL:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=UTF-8'));

How to Convert Curl command to php for -i -t option

Looks like you want to use curl to upload a file...

Example

// initialise the curl request
$request = curl_init('http://example.com/');

// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'file' => '@' . realpath('example.txt')
));

// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);

// close the session
curl_close($request);

Convert Linux Curl to PHP

You asked how the following linux terminal curl command relates to the options of PHP curl:

curl -k -i -H "Content-type: application/x-www-form-urlencoded" -c cookies.txt -X POST https://192.168.100.100:444/appserver/j_spring_security_chec‌​k -d "j_username=admin&j_password=demoserver"

Here is a list of the above options/flags:

  • -k = CURLOPT_SSL_VERIFYPEER: false
  • -i = CURLOPT_HEADER: true
  • -H = CURLOPT_HTTPHEADER
  • -c = CURLOPT_COOKIEJAR + CURLOPT_COOKIEFILE
  • -X POST = CURLOPT_POST: true
  • -d = CURLOPT_POSTFIELDS

Which would lead to exactly the following:

<?php
$ch = curl_init();
$url = "https://192.168.100.100:444/appserver/j_spring_security_chec‌​k";
$postData = 'j_username=admin&j_password=demoserver';
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1); // -X
curl_setopt($ch, CURLOPT_POSTFIELDS,$postData); // -d
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'application/x-www-form-urlencoded'
)); // -H
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); // -c
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); // -c
curl_setopt($ch, CURLOPT_HEADER, true); // -i
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // -k
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // see comment
echo curl_exec ($ch);
curl_close ($ch);

I hope this helps you.



Related Topics



Leave a reply



Submit