How to upload files using multipart in PHP curl simulation post

Source: Internet
Author: User
A 20007 error occurred while using the Sina Weibo API to write a picture bed, and the document said it needed to upload the file with multipart. How do I upload a file using multipart in PHP with the Curl simulation post?

The code I'm using:

$uri = "https://upload.api.weibo.com/2/statuses/upload.json";$data = array (        'access_token' => '',        'status' => '',        'pic' => ''); $ch = curl_init ();curl_setopt ( $ch, CURLOPT_URL, $uri );curl_setopt ( $ch, CURLOPT_POST, 1 );curl_setopt ( $ch, CURLOPT_HEADER, 0 );curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );$return = curl_exec ( $ch );curl_close ( $ch ); print_r($return);

Reply content:

A 20007 error occurred while using the Sina Weibo API to write a picture bed, and the document said it needed to upload the file with multipart. How do I upload a file using multipart in PHP with the Curl simulation post?

The code I'm using:

$uri = "https://upload.api.weibo.com/2/statuses/upload.json";$data = array (        'access_token' => '',        'status' => '',        'pic' => ''); $ch = curl_init ();curl_setopt ( $ch, CURLOPT_URL, $uri );curl_setopt ( $ch, CURLOPT_POST, 1 );curl_setopt ( $ch, CURLOPT_HEADER, 0 );curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );$return = curl_exec ( $ch );curl_close ( $ch ); print_r($return);

Look at Curl code should be no big problem, do not know if you are a few values for the reason of empty,
"Pic" = "@/path/to/myfile.jpg",

When your $data is an array, curl will automatically post it in multipart mode.
In addition, the array must be an array of the K=>V format.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.