data using cURL to API

I am trying to post according to these instructions:

HTTP Method: POST

Authentication: Required

Content Type: multipart/form-data

Accept: application/json

Example Request

POST https://sandbox.domain.com/api/packages/

Request Payload

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

How do I translate that into a php cURL request? Here is what I have now:

    $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/8624.html

上一篇: 我如何删除卷曲发送的默认标题

下一篇: 数据使用cURL到API