Hello. I am writing a script to add posts to the group:

$dataSend = array( 'owner_id' => "-123456", 'message' => "Hello wall.post!", 'attachments'=> "photo-123456_xxxxxxxxx" // where xxxxxxxxx = media_id ) ; $vkAPI->api("wall.post",$dataSend); 

It works, it remains to attach a photo. Tell me how to get media_id ?

  • @ n.osennij hello. for these docs and collected $ dataSend, you need to get <media_id> - ultimatum
  • It also says: <media_id> - ID of the media application. For example: photo100172_166443618, photo-1_265827614. So you need to upload a photo first. - n.osennij
  • For example, you can get a list of photos using the photos.getAll method and use id as <media_id> . - webDev_
  • This is understandable, and then get media_id , which must be inserted into attachments. Just how to do it? I'm trying to figure it out. If anyone has any experience, share w - ultimatum

2 answers 2

Here is the working code:

  $accessToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $vkAPI = new \BW\Vkontakte(['access_token' => $accessToken]); $publicID = -$group_id; // group_id $getURL = array( 'group_id' => $group_id, ); if($vkAPI->api("photos.getWallUploadServer",$getURL)){ // получаем upload_url $result = $vkAPI->api("photos.getWallUploadServer",$getURL); } else { echo "ERROR URI<BR>"; } $curl = curl_init(); //загружаем фото и получаем photo_id и owner_id $file = DIR_IMAGE.'0qDLZ0kw0I.jpg'; // путь к загружаемому файлу $file = curl_file_create($file, mime_content_type($file),pathinfo($file)['basename']); curl_setopt($curl, CURLOPT_URL, $result['response']['upload_url']); curl_setopt($curl, CURLOPT_POST, true); $charset = array( 'Content-Type' => 'multipart/form-data', 'charset' => 'utf-8', ); curl_setopt($curl, CURLOPT_HTTPHEADER, $charset ); curl_setopt($curl, CURLOPT_POSTFIELDS, ['file' => $file]); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 10); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); $response_image = json_decode(curl_exec($curl)); curl_close($curl); $request_params = array( 'group_id'=>"$group_id", 'photo' => $response_image->photo, 'server' => $response_image->server, 'hash' => $response_image->hash, ); $getPhoto = $vkAPI->api("photos.saveWallPhoto",$request_params); $photo_id = $getPhoto['response'][0]['pid']; $owner_id = $getPhoto['response'][0]['owner_id']; $dataSend = array( // подготавливаем наш Post 'owner_id' => $publicID, 'message' => "I'm new Post!", 'attachments'=> "photo".$owner_id."_".$photo_id.", https://google.com/" // where photo_id = media_id ) ; if($vkAPI->api("wall.post",$dataSend)){ $vkAPI->api("wall.post",$dataSend); echo "Post добавлен!<BR>"; } else { echo "ERROR! Ищем ошибку<BR>"; } 

Infa docks described, so I will not re-add. All good!

    To find out this media_id , you must first either upload a photo to the site yourself before posting a recording, or take an already downloaded one.

    To upload a photo you will have to follow a few steps:

    1. Call the photos.getWallUploadServer method, passing it the group_id parameter with the group identifier, a positive number.
    2. In response, the server will send a JSON object with an upload_url field containing the URL for uploading the file. At this address, you must perform a POST request with a photo field that will contain a photo.
    3. After that, it will be necessary to transfer the data obtained in the previous step to the photos.saveWallPhoto method. (If I do not confuse anything, it is enough to pass only the photo parameter).
    4. And only now it makes sense to call wall.post , as in your example.

    The procedure is described in more detail in the documentation on the File Download page.

    Of course, if the photo is already present on the site at the time of publication, then everything is easier. You can peep this meida_id in the link to the photo.