使用cURL到API POST PDF multipart / form-data

时间:2015-05-05 15:57:59

标签: php pdf post curl multipartform-data

我想根据这些说明发帖:

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 );

0 个答案:

没有答案