When sending multipart / form-data to the received url, request

https://pu.vk.com/c639830/upload.php?act=do_add&mid=xxx&aid=-14&gid=0&hash=xxxx&rhash=xxxxx&swfupload=1&api=1&wallphoto=1 

from the VK server comes a semi-correct answer (there is a server, and hash, no photo). Here's how it comes:

 {"server":87899,"photo":"[]","hash":"xxxx"} 

Here is how it is expected:

 { "server":123456, "photos_list":"[{\"photo\":\"e9f2eba71b:y\",\"sizes\":[[\"s\",\"123456852\",\"e65f\",\"Br4ir9YAvO8\",75,41],[\"m\",\"123456852\",\"e660\",\"Lqpe1N8s8zY\",130,71],[\"x\",\"123456852\",\"e661\",\"tRFbnaIP_4c\",604,330],[\"y\",\"123456852\",\"e662\",\"8JhBOy0qR6o\",748,409],[\"o\",\"123456852\",\"e663\",\"fn5KcewNluM\",130,87],[\"p\",\"123456852\",\"e664\",\"ESWOdpl7bvY\",200,133],\"kid\":\"569c3da3b168b347315aa5adc92a953a\",\"debug\":\"xsymyxyyyoypyqyry\"}]", "aid":98754321, "hash":"22b333dbbef7cd9b1f9829b5f8713f86" } 

Test code

 function get_web_page($url, $post = null) { $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // don't return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle all encodings CURLOPT_USERAGENT => "spider", // who am i CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect CURLOPT_TIMEOUT => 120, // timeout on response CURLOPT_MAXREDIRS => 10, // stop after 10 redirects CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks ); $ch = curl_init( $url ); curl_setopt_array( $ch, $options ); if($post){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } $content = curl_exec( $ch ); $err = curl_errno( $ch ); $errmsg = curl_error( $ch ); $header = curl_getinfo( $ch ); curl_close( $ch ); $header['errno'] = $err; $header['errmsg'] = $errmsg; $header['content'] = $content; return $header; } $for_upload = get_web_page("https://api.vk.com/method/photos.getWallUploadServer?access_token=".$token."&group_id".$id); $url_upload = json_decode($for_upload['content'])->response->upload_url; $upload = get_web_page($url_upload, array("file1" => "test.jpg")); 

What could be the problem?

  • What specific request do you send? Add it to your question. - hedgehogues
  • request of the form: pu.vk.com/c639830/… - hitcode

3 answers 3

This code does not transfer the photo itself, but simply transfers the array ['file1' => 'test.jpg'] :

 get_web_page($url_upload, array("file1" => "test.jpg")); 

Judging by this documentation, you should do something like this:

 get_web_page($url_upload, ['photo' => ['file1' => file_get_contents($pathToFile)]]); 
  • really a mistake, thanks. but, the north still returns an empty photo array. - hitcode
 $image_path = "@$photo"; //где $photo - ПОЛНЫЙ путь до файла get_web_page($url_upload, ['photo' => ['file1' => $image_path]]); 

Try this

1.Curl, if I am not mistaken, does not understand the relative paths.

2. To transfer a file, you must put "@" before the file.

    PHP 5.6+ has a new file transfer feature using CURLFile($localFile) instead '@'.$localFile :

     <? $request_params = array( 'image_type' => '510x128', 'access_token' => 'xxx', 'v' => '5.92' ); $t = json_decode(file_get_contents('https://api.vk.com/method/appWidgets.getAppImageUploadServer?'. http_build_query($request_params))); $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data")); curl_setopt($ch, CURLOPT_URL, $t->response->upload_url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_POSTFIELDS, array("image" => new CURLFile(dirname(__FILE__).'\img.jpg'))); curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); $result = json_decode(curl_exec($ch)); curl_close($ch); $request_params = array( 'hash' => $result->hash, 'image' => $result->image, 'access_token' => 'xxx', 'v' => '5.92' ); print_r(file_get_contents('https://api.vk.com/method/appWidgets.saveAppImage?'. http_build_query($request_params))); ?> 
    • Please complete the answer with an explanation of what this code fragment does. - 0xdb
    • In PHP 5.6+, CURLFile ($ localFile), instead of '@'. $ LocalFile. - Go View