Advertisement
halleman017

upload image market

Feb 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1. <?php
  2.  
  3.     echo('<pre>');
  4.  
  5.     // group id
  6.     $group_id = '141035013';
  7.     // access token
  8.     $token = '4c1c5d3a8f2f1e2399349539f6003623b67fdd8ab30e88c2159242e82ee827a1c9bc0152140db3771d9a4';
  9.  
  10.     // params request
  11.     $params = array(
  12.  
  13.             'group_id' => $group_id,
  14.             'access_token' => $token,
  15.             'main_photo' => 1
  16.  
  17.     );
  18.  
  19.     // generic url
  20.     $url = 'https://api.vk.com/method/photos.getMarketUploadServer?'.http_build_query($params);
  21.  
  22.     // generic options curl request
  23.     $options = array(
  24.                        CURLOPT_URL => $url,
  25.                        CURLOPT_RETURNTRANSFER => true
  26.                     );
  27.  
  28.     $response = request($options);
  29.  
  30.     // get url from response
  31.     $url = $response['response']['upload_url'];
  32.  
  33.     // path to photo
  34.     $path = 'upic.jpg';
  35.     // generic post data
  36.     $data = array('file' => '@'.$path);
  37.  
  38.     // generic options curl request
  39.     $options = array(
  40.                       CURLOPT_URL => $url,
  41.                       CURLOPT_POST => true,
  42.                       CURLOPT_POSTFIELDS => $data,
  43.                       CURLOPT_RETURNTRANSFER => true,
  44.                       CURLOPT_HEADERS => array('Content-Type: multipart/form-data')
  45.                     );
  46.    
  47.     // send curl request
  48.     $response = request($options);
  49.  
  50.     // get photo id from response
  51.     $photo = json_decode($response['photo'], true)[0]['photo'];
  52.  
  53.     // options url
  54.     $params = array(
  55.  
  56.             'group_id' => $group_id,
  57.             'access_token' => $token,
  58.             'photo' => $photo,
  59.             'server' => $response['server'],
  60.             'hash' => $response['hash'],
  61.             'crop_data' => $response['crop_data'],
  62.             'crop_hash' => $response['crop_hash']
  63.  
  64.     );
  65.  
  66.     // generic url
  67.     $url = 'https://api.vk.com/method/photos.saveMarketPhoto?'.http_build_query($params);
  68.  
  69.     // options curl params
  70.     $options = array(
  71.                         CURLOPT_URL => $url,
  72.                         CURLOPT_RETURNTRANSFER => true
  73.                     );
  74.  
  75.     // send request
  76.     $response = request($options);
  77.  
  78.     print_r($response);
  79.  
  80.     function request($options)
  81.     {
  82.         $ch = curl_init();
  83.        
  84.         curl_setopt_array($ch, $options);
  85.  
  86.         $response = curl_exec($ch);
  87.  
  88.         curl_close($ch);
  89.  
  90.         return json_decode($response, true);
  91.     }
  92.  
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement