Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP cURL:使用-F选项发出cURL请求_Php_Api_Curl - Fatal编程技术网

PHP cURL:使用-F选项发出cURL请求

PHP cURL:使用-F选项发出cURL请求,php,api,curl,Php,Api,Curl,我使用的是Forte REST API,这里我需要发出如下的cURL请求:- 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

我使用的是Forte REST API,这里我需要发出如下的cURL请求:-

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"
我尝试通过以下方式将其转换为PHP cURL请求:-

<?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);
?>
根据他们的文档,该请求将生成一个到Forte的HTTP请求,类似于以下内容:

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--
POST/api/v3/organizations/org_300005/documents HTTP/1.1
主机:sandbox.forte.net
用户代理:curl/7.46.0
接受:application/json;字符集=utf-8
内容类型:多部分/混合;边界=--abcdefghijklmnopqrstuvxyz
内容长度:8469
授权:基本{编码的APIAccessID:APICureKey字符串}
X-Forte-Auth-Organization-Id:org_300005
--abcdefghijklmnopqrstuvwxyz
内容配置:表单数据;filename=“filename.jpg”
内容类型:图像/jpeg
--abcdefghijklmnopqrstuvwxyz
内容类型:application/json
{“资源”:“应用程序”,“资源id”:“应用程序103448”,“说明”:“接收”}
--abcdefghijklmnopqrstuvwxyz--

Forte API原始链接是:

您的代码缺少一些他们期望的标题:

$headers[] = 'User-Agent: curl/7.46.0';
$headers[] = 'Accept: application/json; charset=utf-8';
$headers[] = 'Content-Type: multipart/mixed; boundary=--abcdefghijklmnopqrstuvwxyz';
尤其是他们对明星的抱怨:

请求正文中缺少内容类型


向代码中添加如上所述的标题以解决问题,请确保将
边界=--abcdefghijklmnopqrstuvwxyz
示例更改为您使用的实际边界。

CURLOPT_VERBOSE
以调试您发送的内容(以及被投诉的是POST-payload还是multipart部分)。现在显示-{“response”:{“环境”:“sandbox”,“response_desc”:“请求正文中未收到任何数据。请验证请求中是否填充了以下内容:填充了内容类型http标头、填充了内容长度http标头、填充了请求正文。”}}}您能否解释一下这里的边界是什么,它们实际上是什么意思?
$headers[] = 'User-Agent: curl/7.46.0';
$headers[] = 'Accept: application/json; charset=utf-8';
$headers[] = 'Content-Type: multipart/mixed; boundary=--abcdefghijklmnopqrstuvwxyz';