From a69374f13183f58869f73049c9ad729b6f82ad4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A6=D1=83=D1=80=D0=BA=D0=B0=D0=BD=20=D0=90=D0=BB=D0=B5?= =?UTF-8?q?=D0=BA=D1=81=D0=B5=D0=B9?= <49027190+alexeitsurkan@users.noreply.github.com> Date: Fri, 23 Sep 2022 12:07:01 +0300 Subject: [PATCH 1/4] Update Ads.php add TargetPixel methods --- src/VK/Actions/Ads.php | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/VK/Actions/Ads.php b/src/VK/Actions/Ads.php index 97e033f..ff3617a 100644 --- a/src/VK/Actions/Ads.php +++ b/src/VK/Actions/Ads.php @@ -637,4 +637,48 @@ public function updateClients($access_token, array $params = []) { public function updateTargetGroup($access_token, array $params = []) { return $this->request->post('ads.updateTargetGroup', $access_token, $params); } + + /** + * @param $access_token + * @param array $params + * @return array|mixed + * @throws VKApiException + * @throws VKClientException + */ + public function getTargetPixels($access_token, array $params = []) { + return $this->request->post('ads.getTargetPixels', $access_token, $params); + } + + /** + * @param $access_token + * @param array $params + * @return array|mixed + * @throws VKApiException + * @throws VKClientException + */ + public function createTargetPixel($access_token, array $params = []) { + return $this->request->post('ads.createTargetPixel', $access_token, $params); + } + + /** + * @param $access_token + * @param array $params + * @return array|mixed + * @throws VKApiException + * @throws VKClientException + */ + public function deleteTargetPixel($access_token, array $params = []) { + return $this->request->post('ads.deleteTargetPixel', $access_token, $params); + } + + /** + * @param $access_token + * @param array $params + * @return array|mixed + * @throws VKApiException + * @throws VKClientException + */ + public function updateTargetPixel($access_token, array $params = []) { + return $this->request->post('ads.updateTargetPixel', $access_token, $params); + } } From 219870168d690c38005c90d4d6879c3c85a4a3e5 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 26 Sep 2022 14:47:50 +0300 Subject: [PATCH 2/4] flood control repeat request after one second --- src/VK/Client/VKApiRequest.php | 56 ++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/VK/Client/VKApiRequest.php b/src/VK/Client/VKApiRequest.php index 943e84c..d997149 100755 --- a/src/VK/Client/VKApiRequest.php +++ b/src/VK/Client/VKApiRequest.php @@ -3,21 +3,24 @@ namespace VK\Client; use VK\Exceptions\Api\ExceptionMapper; +use VK\Exceptions\Api\VKApiFloodException; +use VK\Exceptions\Api\VKApiTooManyException; use VK\Exceptions\VKApiException; use VK\Exceptions\VKClientException; use VK\TransportClient\Curl\CurlHttpClient; use VK\TransportClient\TransportClientResponse; use VK\TransportClient\TransportRequestException; -class VKApiRequest { - private const PARAM_VERSION = 'v'; +class VKApiRequest +{ + private const PARAM_VERSION = 'v'; private const PARAM_ACCESS_TOKEN = 'access_token'; - private const PARAM_LANG = 'lang'; + private const PARAM_LANG = 'lang'; - private const KEY_ERROR = 'error'; + private const KEY_ERROR = 'error'; private const KEY_RESPONSE = 'response'; - protected const CONNECTION_TIMEOUT = 10; + protected const CONNECTION_TIMEOUT = 10; protected const HTTP_STATUS_CODE_OK = 200; /** @@ -46,11 +49,12 @@ class VKApiRequest { * @param string|null $language * @param string $host */ - public function __construct(string $api_version, ?string $language, string $host) { + public function __construct(string $api_version, ?string $language, string $host) + { $this->http_client = new CurlHttpClient(static::CONNECTION_TIMEOUT); - $this->version = $api_version; - $this->host = $host; - $this->language = $language; + $this->version = $api_version; + $this->host = $host; + $this->language = $language; } /** @@ -65,8 +69,9 @@ public function __construct(string $api_version, ?string $language, string $host * @throws VKClientException * @throws VKApiException */ - public function post(string $method, string $access_token, array $params = array()) { - $params = $this->formatParams($params); + public function post(string $method, string $access_token, array $params = []) + { + $params = $this->formatParams($params); $params[static::PARAM_ACCESS_TOKEN] = $access_token; if (!isset($params[static::PARAM_VERSION])) { @@ -79,8 +84,12 @@ public function post(string $method, string $access_token, array $params = array $url = $this->host . '/' . $method; + try { $response = $this->http_client->post($url, $params); + } catch (VKApiFloodException|VKApiTooManyException $e) { + sleep(1); + $response = $this->http_client->post($url, $params); } catch (TransportRequestException $e) { throw new VKClientException($e); } @@ -100,7 +109,8 @@ public function post(string $method, string $access_token, array $params = array * @throws VKClientException * @throws VKApiException */ - public function upload(string $upload_url, string $parameter_name, string $path) { + public function upload(string $upload_url, string $parameter_name, string $path) + { try { $response = $this->http_client->upload($upload_url, $parameter_name, $path); } catch (TransportRequestException $e) { @@ -120,14 +130,15 @@ public function upload(string $upload_url, string $parameter_name, string $path) * @throws VKApiException * @throws VKClientException */ - private function parseResponse(TransportClientResponse $response) { + private function parseResponse(TransportClientResponse $response) + { $this->checkHttpStatus($response); - $body = $response->getBody(); + $body = $response->getBody(); $decode_body = $this->decodeBody($body); if (isset($decode_body[static::KEY_ERROR])) { - $error = $decode_body[static::KEY_ERROR]; + $error = $decode_body[static::KEY_ERROR]; $api_error = new VKApiError($error); throw ExceptionMapper::parse($api_error); } @@ -146,12 +157,15 @@ private function parseResponse(TransportClientResponse $response) { * * @return array */ - private function formatParams(array $params) { + private function formatParams(array $params) + { foreach ($params as $key => $value) { if (is_array($value)) { $params[$key] = implode(',', $value); - } else if (is_bool($value)) { - $params[$key] = $value ? 1 : 0; + } else { + if (is_bool($value)) { + $params[$key] = $value ? 1 : 0; + } } } return $params; @@ -164,7 +178,8 @@ private function formatParams(array $params) { * * @return mixed */ - protected function decodeBody(string $body) { + protected function decodeBody(string $body) + { $decoded_body = json_decode($body, true); if ($decoded_body === null || !is_array($decoded_body)) { @@ -179,7 +194,8 @@ protected function decodeBody(string $body) { * * @throws VKClientException */ - protected function checkHttpStatus(TransportClientResponse $response): void { + protected function checkHttpStatus(TransportClientResponse $response): void + { if ((int)$response->getHttpStatus() !== static::HTTP_STATUS_CODE_OK) { throw new VKClientException("Invalid http status: {$response->getHttpStatus()}"); } From 9190a90a7a2431554f49784897d93bd44f24b844 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 26 Sep 2022 14:47:50 +0300 Subject: [PATCH 3/4] flood control repeat request after one second --- src/VK/Client/VKApiRequest.php | 71 +++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/src/VK/Client/VKApiRequest.php b/src/VK/Client/VKApiRequest.php index 943e84c..e9b5625 100755 --- a/src/VK/Client/VKApiRequest.php +++ b/src/VK/Client/VKApiRequest.php @@ -3,21 +3,24 @@ namespace VK\Client; use VK\Exceptions\Api\ExceptionMapper; +use VK\Exceptions\Api\VKApiFloodException; +use VK\Exceptions\Api\VKApiTooManyException; use VK\Exceptions\VKApiException; use VK\Exceptions\VKClientException; use VK\TransportClient\Curl\CurlHttpClient; use VK\TransportClient\TransportClientResponse; use VK\TransportClient\TransportRequestException; -class VKApiRequest { - private const PARAM_VERSION = 'v'; +class VKApiRequest +{ + private const PARAM_VERSION = 'v'; private const PARAM_ACCESS_TOKEN = 'access_token'; - private const PARAM_LANG = 'lang'; + private const PARAM_LANG = 'lang'; - private const KEY_ERROR = 'error'; + private const KEY_ERROR = 'error'; private const KEY_RESPONSE = 'response'; - protected const CONNECTION_TIMEOUT = 10; + protected const CONNECTION_TIMEOUT = 10; protected const HTTP_STATUS_CODE_OK = 200; /** @@ -46,11 +49,12 @@ class VKApiRequest { * @param string|null $language * @param string $host */ - public function __construct(string $api_version, ?string $language, string $host) { + public function __construct(string $api_version, ?string $language, string $host) + { $this->http_client = new CurlHttpClient(static::CONNECTION_TIMEOUT); - $this->version = $api_version; - $this->host = $host; - $this->language = $language; + $this->version = $api_version; + $this->host = $host; + $this->language = $language; } /** @@ -65,8 +69,9 @@ public function __construct(string $api_version, ?string $language, string $host * @throws VKClientException * @throws VKApiException */ - public function post(string $method, string $access_token, array $params = array()) { - $params = $this->formatParams($params); + public function post(string $method, string $access_token, array $params = []) + { + $params = $this->formatParams($params); $params[static::PARAM_ACCESS_TOKEN] = $access_token; if (!isset($params[static::PARAM_VERSION])) { @@ -79,13 +84,18 @@ public function post(string $method, string $access_token, array $params = array $url = $this->host . '/' . $method; - try { - $response = $this->http_client->post($url, $params); - } catch (TransportRequestException $e) { - throw new VKClientException($e); - } - return $this->parseResponse($response); + while (true) { + try { + $response = $this->http_client->post($url, $params); + return $this->parseResponse($response); + } catch (TransportRequestException $e) { + throw new VKClientException($e); + } catch (VKApiFloodException|VKApiTooManyException $e) { + sleep(1); + continue; + } + } } /** @@ -100,7 +110,8 @@ public function post(string $method, string $access_token, array $params = array * @throws VKClientException * @throws VKApiException */ - public function upload(string $upload_url, string $parameter_name, string $path) { + public function upload(string $upload_url, string $parameter_name, string $path) + { try { $response = $this->http_client->upload($upload_url, $parameter_name, $path); } catch (TransportRequestException $e) { @@ -120,14 +131,15 @@ public function upload(string $upload_url, string $parameter_name, string $path) * @throws VKApiException * @throws VKClientException */ - private function parseResponse(TransportClientResponse $response) { + private function parseResponse(TransportClientResponse $response) + { $this->checkHttpStatus($response); - $body = $response->getBody(); + $body = $response->getBody(); $decode_body = $this->decodeBody($body); if (isset($decode_body[static::KEY_ERROR])) { - $error = $decode_body[static::KEY_ERROR]; + $error = $decode_body[static::KEY_ERROR]; $api_error = new VKApiError($error); throw ExceptionMapper::parse($api_error); } @@ -146,12 +158,15 @@ private function parseResponse(TransportClientResponse $response) { * * @return array */ - private function formatParams(array $params) { + private function formatParams(array $params) + { foreach ($params as $key => $value) { if (is_array($value)) { $params[$key] = implode(',', $value); - } else if (is_bool($value)) { - $params[$key] = $value ? 1 : 0; + } else { + if (is_bool($value)) { + $params[$key] = $value ? 1 : 0; + } } } return $params; @@ -164,7 +179,8 @@ private function formatParams(array $params) { * * @return mixed */ - protected function decodeBody(string $body) { + protected function decodeBody(string $body) + { $decoded_body = json_decode($body, true); if ($decoded_body === null || !is_array($decoded_body)) { @@ -179,7 +195,10 @@ protected function decodeBody(string $body) { * * @throws VKClientException */ - protected function checkHttpStatus(TransportClientResponse $response): void { + protected + function checkHttpStatus( + TransportClientResponse $response + ): void { if ((int)$response->getHttpStatus() !== static::HTTP_STATUS_CODE_OK) { throw new VKClientException("Invalid http status: {$response->getHttpStatus()}"); } From 25d891ed64c2f99661dbe13e5d1fe3a73305236b Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 29 Sep 2022 12:22:31 +0300 Subject: [PATCH 4/4] new Action LeadForms --- src/VK/Actions/LeadForms.php | 106 ++++++++++++++++++++++++++ src/VK/Actions/Leads.php | 137 ---------------------------------- src/VK/Client/VKApiClient.php | 33 ++------ 3 files changed, 114 insertions(+), 162 deletions(-) create mode 100644 src/VK/Actions/LeadForms.php delete mode 100644 src/VK/Actions/Leads.php diff --git a/src/VK/Actions/LeadForms.php b/src/VK/Actions/LeadForms.php new file mode 100644 index 0000000..876cf84 --- /dev/null +++ b/src/VK/Actions/LeadForms.php @@ -0,0 +1,106 @@ +request = $request; + } + + /** + * @param $access_token + * @param array $params + * @return array|mixed + * @throws \VK\Exceptions\VKApiException + * @throws \VK\Exceptions\VKClientException + */ + public function create($access_token, array $params = []) + { + return $this->request->post('leadForms.create', $access_token, $params); + } + + /** + * @param $access_token + * @param array $params + * @return array|mixed + * @throws \VK\Exceptions\VKApiException + * @throws \VK\Exceptions\VKClientException + */ + public function delete($access_token, array $params = []) + { + return $this->request->post('leadForms.delete', $access_token, $params); + } + + /** + * @param $access_token + * @param array $params + * @return array|mixed + * @throws \VK\Exceptions\VKApiException + * @throws \VK\Exceptions\VKClientException + */ + public function get($access_token, array $params = []) + { + return $this->request->post('leadForms.get', $access_token, $params); + } + + /** + * @param $access_token + * @param array $params + * @return array|mixed + * @throws \VK\Exceptions\VKApiException + * @throws \VK\Exceptions\VKClientException + */ + public function getLeads($access_token, array $params = []) + { + return $this->request->post('leadForms.getLeads', $access_token, $params); + } + + /** + * @param $access_token + * @return array|mixed + * @throws \VK\Exceptions\VKApiException + * @throws \VK\Exceptions\VKClientException + */ + public function getUploadURL($access_token) + { + return $this->request->post('leadForms.getUploadURL', $access_token); + } + + /** + * @param $access_token + * @param array $params + * @return array|mixed + * @throws \VK\Exceptions\VKApiException + * @throws \VK\Exceptions\VKClientException + */ + public function list($access_token, array $params = []) + { + return $this->request->post('leadForms.list', $access_token, $params); + } + + /** + * @param $access_token + * @param array $params + * @return array|mixed + * @throws \VK\Exceptions\VKApiException + * @throws \VK\Exceptions\VKClientException + */ + public function update($access_token, array $params = []) + { + return $this->request->post('leadForms.update', $access_token, $params); + } +} diff --git a/src/VK/Actions/Leads.php b/src/VK/Actions/Leads.php deleted file mode 100644 index 6d9ab2e..0000000 --- a/src/VK/Actions/Leads.php +++ /dev/null @@ -1,137 +0,0 @@ -request = $request; - } - - /** - * Checks if the user can start the lead. - * - * @param string $access_token - * @param array $params - * - @var integer lead_id: Lead ID. - * - @var integer test_result: Value to be return in 'result' field when test mode is used. - * - @var boolean test_mode - * - @var boolean auto_start - * - @var integer age: User age. - * - @var string country: User country code. - * @throws VKClientException - * @throws VKApiException - * @throws VKApiActionFailedException Unable to process action - * @return mixed - */ - public function checkUser($access_token, array $params = []) { - return $this->request->post('leads.checkUser', $access_token, $params); - } - - /** - * Completes the lead started by user. - * - * @param string $access_token - * @param array $params - * - @var string vk_sid: Session obtained as GET parameter when session started. - * - @var string secret: Secret key from the lead testing interface. - * - @var string comment: Comment text. - * @throws VKClientException - * @throws VKApiException - * @throws VKApiLimitsException Out of limits - * @throws VKApiVotesException Not enough votes - * @return mixed - */ - public function complete($access_token, array $params = []) { - return $this->request->post('leads.complete', $access_token, $params); - } - - /** - * Returns lead stats data. - * - * @param string $access_token - * @param array $params - * - @var integer lead_id: Lead ID. - * - @var string secret: Secret key obtained from the lead testing interface. - * - @var string date_start: Day to start stats from (YYYY_MM_DD, e.g.2011-09-17). - * - @var string date_end: Day to finish stats (YYYY_MM_DD, e.g.2011-09-17). - * @throws VKClientException - * @throws VKApiException - * @return mixed - */ - public function getStats($access_token, array $params = []) { - return $this->request->post('leads.getStats', $access_token, $params); - } - - /** - * Returns a list of last user actions for the offer. - * - * @param string $access_token - * @param array $params - * - @var integer offer_id: Offer ID. - * - @var string secret: Secret key obtained in the lead testing interface. - * - @var integer offset: Offset needed to return a specific subset of results. - * - @var integer count: Number of results to return. - * - @var LeadsStatus status: Action type. Possible values: *'0' — start,, *'1' — finish,, *'2' — blocking users,, *'3' — start in a test mode,, *'4' — finish in a test mode. - * - @var boolean reverse: Sort order. Possible values: *'1' — chronological,, *'0' — reverse chronological. - * @throws VKClientException - * @throws VKApiException - * @return mixed - */ - public function getUsers($access_token, array $params = []) { - return $this->request->post('leads.getUsers', $access_token, $params); - } - - /** - * Counts the metric event. - * - * @param string $access_token - * @param array $params - * - @var string data: Metric data obtained in the lead interface. - * @throws VKClientException - * @throws VKApiException - * @return mixed - */ - public function metricHit($access_token, array $params = []) { - return $this->request->post('leads.metricHit', $access_token, $params); - } - - /** - * Creates new session for the user passing the offer. - * - * @param string $access_token - * @param array $params - * - @var integer lead_id: Lead ID. - * - @var string secret: Secret key from the lead testing interface. - * - @var integer uid - * - @var integer aid - * - @var boolean test_mode - * - @var boolean force - * @throws VKClientException - * @throws VKApiException - * @throws VKApiLimitsException Out of limits - * @return mixed - */ - public function start($access_token, array $params = []) { - return $this->request->post('leads.start', $access_token, $params); - } -} diff --git a/src/VK/Client/VKApiClient.php b/src/VK/Client/VKApiClient.php index 03f5b34..1849926 100755 --- a/src/VK/Client/VKApiClient.php +++ b/src/VK/Client/VKApiClient.php @@ -14,7 +14,7 @@ use VK\Actions\Friends; use VK\Actions\Gifts; use VK\Actions\Groups; -use VK\Actions\Leads; +use VK\Actions\LeadForms; use VK\Actions\Likes; use VK\Actions\Market; use VK\Actions\Messages; @@ -24,7 +24,6 @@ use VK\Actions\Orders; use VK\Actions\Pages; use VK\Actions\Photos; -use VK\Actions\Places; use VK\Actions\Polls; use VK\Actions\Search; use VK\Actions\Secure; @@ -104,9 +103,9 @@ class VKApiClient { private $groups; /** - * @var Leads + * @var LeadForms */ - private $leads; + private $leadForms; /** * @var Likes @@ -153,11 +152,6 @@ class VKApiClient { */ private $photos; - /** - * @var Places - */ - private $places; - /** * @var Polls */ @@ -361,14 +355,14 @@ public function groups(): Groups { } /** - * @return Leads + * @return LeadForms */ - public function leads(): Leads { - if (!$this->leads) { - $this->leads = new Leads($this->request); + public function leadForms(): LeadForms { + if (!$this->leadForms) { + $this->leadForms = new LeadForms($this->request); } - return $this->leads; + return $this->leadForms; } /** @@ -470,17 +464,6 @@ public function photos(): Photos { return $this->photos; } - /** - * @return Places - */ - public function places(): Places { - if (!$this->places) { - $this->places = new Places($this->request); - } - - return $this->places; - } - /** * @return Polls */