mtindall89
mtindall89

Reputation: 419

Convert a CURL command to PHP Curl

I Have the following command I can run in PHP and it works as expected:

$params = [
        'file' => $xmlLocalPath,
        'type' => 'mismo',
        'file_type' => $xmlFile['file_type'],
        'filename' => $xmlFile['file_name']
    ];

$cmd =
        'curl -X POST ' . $this->outboundBaseURL . 'document \
        -H "Accept: application/json" \
        -H "Authorization: Bearer ' . $this->token . '" \
        -H "Cache-Control: no-cache" \
        -H "Content-Type: multipart/form-data" \
        -F "file=@' . $params['file'] . ';type=' . $params['file_type'] . '" \
        -F "type=' . $params['type'] . '" \
        -F "filename=' . $params['filename'] . '"';
    exec($cmd, $result);

I need to get this to work using PHP's curl library, but I can't get it to work quite right. I'm on PHP 5.6, and here's what I have right now:

$params = [
        'file' => $xmlLocalPath,
        'type' => 'mismo',
        'file_type' => $xmlFile['file_type'],
        'filename' => $xmlFile['file_name']
    ];

    $ch = curl_init();

    curl_setopt_array($ch, [
        CURLOPT_URL => $this->outboundBaseURL . 'document',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 60,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $params,
        CURLOPT_HTTPHEADER => [
            'Accept: application/json',
            'Authorization: Bearer ' . $this->token,
            'Cache-Control: no-cache',
            'Content-Type: multipart/form-data'
        ]
    ]);

    curl_setopt($ch, CURLINFO_HEADER_OUT, true); // enable tracking

    $response = curl_exec($ch);
    $err = curl_error($ch);

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

I know the issue has to be with POSTFIELDS, and the file in particular, but I'm not show how to give PHP CURL the parameters in a proper way such that the file and other parameters will send just as the do in the raw curl call. I understand that the "@" method is deprecated in PHP Curl and I've also tried using curl_file_create with the local file with no success

Upvotes: 0

Views: 298

Answers (1)

Leprechaun
Leprechaun

Reputation: 819

Try adding one more parameter:

CURLOPT_POST => 1

Upvotes: 1

Related Questions