简体   繁体   中英

Upload image with PHP curl with parameters gives '415'

This is the description of what I need to do (I know it's vague, but it's all I got) :

Images can be uploaded by sending an HTTP PUT request to the image URI. The body of the request should contain the image data or if the last modified date of the image is relevant, an XML or JSON document wrapping the image. In the latter case the XML root node should be called “image” and it should contain the nodes “lastModified” (specifiying the date as a Unix timestamp) and “data” containing the image data. A JSON representation should contain the two values as object fields. The content type should be "application/x-www-form-urlencoded" and the request should include the form parameters filesize, filename, dir and lastmodified. The response will be an ActionResponse and will inform the Client about any problems that might occur if an upload is attempted.

This is my code thus far :

function addImage($path) {
    global $serviceURL;
    global $curlhandle;

    $request_headers = array("X-Requested-With: XMLHttpRequest", "Accept: application/json", "Content-Type: application/x-www-form-urlencoded; charset=UTF-8");

    curl_setopt($curlhandle, CURLOPT_POSTFIELDS, $request_param);
    curl_setopt($curlhandle, CURLOPT_HTTPHEADER, $request_headers);
    curl_setopt($curlhandle, CURLOPT_PUT, 1);
    curl_setopt($curlhandle, CURLOPT_URL, $serviceURL."image/".$path);
    $post = array(
        "file_box" => "@/Users/steven/Desktop/test.jpg"
    );
    curl_setopt($curlhandle, CURLOPT_POSTFIELDS, $post);
    $data=curl_exec($curlhandle);   
    var_dump($data);
}

This codes ends up in giving me the following error:

HTTP/1.1 415 Unsupported Media Type

Anyone who could help out?

Your description says you should send a HTTP PUT, but your example sends a multipart formpost using POST.

The description also mentions using XML and JSON while your code seems to not even attempt to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM