0

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

1
  • Have you looked at the example here? Commented Jul 9, 2018 at 21:04

1 Answer 1

1

Try adding one more parameter:

CURLOPT_POST => 1
Sign up to request clarification or add additional context in comments.

1 Comment

Wow.......I never would have thought I'd need CURLOPT_POST => 1 and CURLOPT_CUSTOMREQUEST => "POST", (the latter being in Postman when I loaded up this API, but CURLOPT_POST was not). Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.