简体   繁体   中英

PHP cURL: Make a cURL request using -F option

I'm using Forte REST API, and here I need to make a cURL request like below:-

curl 
    -H "Authorization: Basic {encoded APIAccessID:APISecureKey string}" 
    -H "X-Forte-Auth-Organization-Id: org_300005" 
    -F document={"resource":"application","resource_id":"app_103448","description":"receipt"};type=application/json 
    -F file:@filename.jpg
"/organizations/org_300005/documents"

I tried to convert it into PHP cURL request by the way of this:-

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://sandbox.forte.net/api/v3/organizations/org_380481/documents');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

$headers = array();
$headers[] = 'Authorization: Basic {auth_key}';
$headers[] = 'X-Forte-Auth-Organization-Id: org_380481';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt(
    $ch,
    CURLOPT_POSTFIELDS,
    array(
      'file' => '@' . realpath('example.txt'),
      'document' => '{"resource":"application","resource_id":"app_103448","description":"receipt"};type=application/json'
    ));

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

var_dump($result);
?>

But while I'm trying it, it's giving a response like:-

{"response":
    {"environment":"sandbox",
    "response_desc":"Content-Type is missing in the request body."}
}

According to their document, the request would generate a HTTP request to Forte similar to the following:

POST /api/v3/organizations/org_300005/documents HTTP/1.1
Host: sandbox.forte.net
User-Agent: curl/7.46.0
Accept: application/json; charset=utf-8
Content-Type: multipart/mixed; boundary=--abcdefghijklmnopqrstuvwxyz
Content-Length: 8469
Authorization: Basic {encoded APIAccessID:APISecureKey string} 
X-Forte-Auth-Organization-Id: org_300005
--abcdefghijklmnopqrstuvwxyz
Content-Disposition:form-data; filename="filename.jpg"
Content-Type: image/jpeg
<binary content of filename.jpg>
--abcdefghijklmnopqrstuvwxyz
Content-Type: application/json
{"resource":"application","resource_id":"app_103448","description":"reciept"}
--abcdefghijklmnopqrstuvwxyz--

The Forte API original link is: https://restdocs.forte.net/?version=latest#379aee3b-bf50-4352-bbb4-95e4d2bbde84

Your code is missing some headers they expect:

$headers[] = 'User-Agent: curl/7.46.0';
$headers[] = 'Accept: application/json; charset=utf-8';
$headers[] = 'Content-Type: multipart/mixed; boundary=--abcdefghijklmnopqrstuvwxyz';

Especially something they complain on minssing:

Content-Type is missing in the request body

Add headers as above to your code to fix the problem, make sure the boundary=--abcdefghijklmnopqrstuvwxyz example is changed to the actual boundary you use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM