Sheng Long Sheng Long - 2 months ago 13
PHP Question

Convert curl command to PHP cURL - kaaProject

curl -v -S -u devuser:devuser123 \
-F 'notification={"applicationId":"32768","schemaId":"32771","topicId":"32768","type":"USER"};type=application/json' \
-F file=@notification.json \
'http://localhost:8080/kaaAdmin/rest/api/sendNotification'


I tried to convert it into PHP and ended like this:

$notification = array("applicationId" =>"32768",
"schemaId"=>"32771",
"topicId"=>"32768",
"type"=>"USER");
$ch = curl_init();
$headers = array();
$headers[] = 'Authorization: Basic ZGV2dXNlcjpkZXZ1c2VyMTIz';
$headers[] = 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryzeZR8KqAYJyI2jPL';
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, 'notification='.json_encode($notification).'&file='.realpath('notification.json'));
$content = curl_exec($ch);
print $content;
curl_close($ch);


I don't know how to set parameter "notification" and 'file'.
please give me a solution.

Thank you!

Answer

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