数据使用cURL到API
我正在尝试按照以下说明进行发布:
HTTP方法:POST
认证:必需
内容类型:multipart / form-data
接受:application / json
示例请求
POST https://sandbox.domain.com/api/packages/
请求负载
------WebKitFormBoundary1bNO60n7FqP5WO4t
Content-Disposition: form-data; name="file"; filename="XR9MN8T9.pdf"
Content-Type: application/pdf
------WebKitFormBoundary1bNO60n7FqP5WO4t
Content-Disposition: form-data; name="payload"
{
"documents":[
{
"name":"PDF 01"
}
]
}
------WebKitFormBoundary1bNO60n7FqP5WO4t--
我如何将它转换成php cURL请求? 这是我现在拥有的:
$fileURL = "/XR9MN8T9.pdf";
//$file = file_get_contents( $fileURL );
$parameters = array(
'file'=> "@" . $fileURL,
'filename' => "XR9MN8T9.pdf",
'Content-Type' => 'application/pdf',
'payload' => array(
'documents' => array(
"PDF 01"
),
),
);
$parameters = json_encode( $parameters );
$curl = curl_init();
$header = array( "Content-Type: multipart/form-data", "Accept: application/json", "Authorization: Basic " . API_KEY );
curl_setopt( $curl, CURLOPT_URL, "https://sandbox.domain.com/api/packages" );
curl_setopt( $curl, CURLOPT_POST, count( $parameters ) );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $parameters );
curl_setopt( $curl, CURLOPT_HTTPHEADER, $header );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
$response = curl_exec( $curl );
curl_close( $curl );
链接地址: http://www.djcxy.com/p/8623.html