1+ <?
2+ class VKapi {
3+ private $ configVKapi ;
4+
5+ function __construct ($ configVKapi ) {
6+ $ this ->config = $ configVKapi ;
7+ }
8+
9+ public function getSocNetName () {
10+ return $ this ->config ['socNetName ' ];
11+ }
12+
13+ function preparePost ($ rssItem ) {
14+
15+ /* Подготавливаем новый пост */
16+ $ postParams = array (
17+ 'owner_id ' => $ this ->config ['vkPublicID ' ],
18+ 'from_group ' => $ this ->config ['fromGroup ' ],
19+ 'message ' => $ this ->config ['postTemplate ' ]($ rssItem ),
20+ );
21+
22+ // Если есть картинка, то скачиваем ее к себе на сервер и заливаем на сервер VK
23+ // (чтобы приаттачить ее по полученному в ответе id).
24+ if ($ rssItem ['postImage ' ]) {
25+ $ imgDownUpResponse = $ this ->imgDownloadUpload ($ rssItem ['postImage ' ], $ this ->config ['vkPublicID ' ]);
26+ if ($ imgDownUpResponse [0 ]->id ) {
27+ $ postParams ['attachments ' ] = "{$ imgDownUpResponse [0 ]->id }" ;
28+ }
29+ }
30+
31+ return $ postParams ;
32+ }
33+
34+ function doPost ($ preparatedPost ) {
35+ $ newVkPostResponse = $ this ->api ('wall.post ' , $ preparatedPost );
36+ if ($ newVkPostResponse ->post_id ) {
37+ $ postResult = array (
38+ 'error ' => false ,
39+ 'response ' => $ newVkPostResponse
40+ );
41+ } else {
42+ $ postResult = array (
43+ 'error ' => true ,
44+ 'response ' => $ newVkPostResponse
45+ );
46+ }
47+ return $ postResult ;
48+ }
49+
50+
51+ public function imgDownloadUpload ($ img , $ publicID ) {
52+ /* Download image */
53+ $ ch = curl_init ($ img );
54+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
55+ $ imgDown = curl_exec ($ ch );
56+ curl_close ($ ch );
57+ $ imgfn = uniqid ().'.jpg ' ;
58+ file_put_contents ($ imgfn , $ imgDown );
59+ if (strpos (mime_content_type ($ imgfn ), 'image ' ) === false ) {
60+ echo 'mime_content_type: ' .mime_content_type ($ imgfn );
61+ echo "\n Не удалось получить изображение. Возможно, защита от хотлинка \n" ;
62+ unlink ($ imgfn );
63+ return false ;
64+ }
65+
66+ /* Upload image */
67+ $ imgUpResponse = $ this ->api ('photos.getWallUploadServer ' , array ('group_id ' => abs ($ publicID )));
68+ $ uploadURL = $ imgUpResponse ->upload_url ;
69+ $ fullServerPathToImage = $ imgfn ;
70+ $ output = array ();
71+ exec ("curl -X POST -F 'photo=@ $ fullServerPathToImage' ' $ uploadURL' " , $ output );
72+ $ imgUpResponse = json_decode ($ output [0 ]);
73+ $ imgUpResponse = $ this ->api ('photos.saveWallPhoto ' , array (
74+ 'group_id ' => abs ($ publicID ),
75+ 'photo ' => $ imgUpResponse ->photo ,
76+ 'server ' => $ imgUpResponse ->server ,
77+ 'hash ' => $ imgUpResponse ->hash ,
78+ ));
79+
80+ unlink ($ imgfn );
81+ return $ imgUpResponse ;
82+ }
83+
84+ public function api ($ method , array $ query = array ()) {
85+ /* Generate query string from array */
86+ $ parameters = array ();
87+ foreach ($ query as $ param => $ value ) {
88+ $ q = $ param . '= ' ;
89+ if (is_array ($ value )) {
90+ $ q .= urlencode (implode (', ' , $ value ));
91+ } else {
92+ $ q .= urlencode ($ value );
93+ }
94+ $ parameters [] = $ q ;
95+ }
96+ $ q = implode ('& ' , $ parameters );
97+ if (count ($ query ) > 0 ) {
98+ $ q .= '& ' ; // Add "&" sign for access_token if query exists
99+ }
100+ $ url = 'https://api.vk.com/method/ ' . $ method . '? ' . $ q . 'access_token= ' . $ this ->config ['vkAccessToken ' ];
101+ $ result = json_decode ($ this ->curl ($ url ));
102+ if (isset ($ result ->response )) {
103+ return $ result ->response ;
104+ }
105+ return $ result ;
106+ }
107+
108+ private function curl ($ url ) {
109+ $ ch = curl_init ();
110+ curl_setopt ($ ch , CURLOPT_URL , $ url );
111+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , TRUE );
112+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , FALSE );
113+ $ result = curl_exec ($ ch );
114+ if (!$ result ) {
115+ $ errno = curl_errno ($ ch );
116+ $ error = curl_error ($ ch );
117+ }
118+ curl_close ($ ch );
119+ if (isset ($ errno ) && isset ($ error )) {
120+ throw new \Exception ($ error , $ errno );
121+ }
122+ return $ result ;
123+ }
124+
125+ }
126+ ?>
0 commit comments