VKCOM / vk-php-sdk

PHP library for working with VK API
MIT License
204 stars 93 forks source link

Ошибка в wall.post #106

Open pro100andreika opened 1 year ago

pro100andreika commented 1 year ago

Не могу передать фото, ошибка сервера что то связанное с curl

VK\TransportClient\TransportRequestException: Failed curl request. Curl error 26. in C:\OSPanel\domains\worldstories.local\vendor\vkcom\vk-php-sdk\src\VK\TransportClient\Curl\CurlHttpClient.php:114
Stack trace:
#0 C:\OSPanel\domains\worldstories.local\vendor\vkcom\vk-php-sdk\src\VK\TransportClient\Curl\CurlHttpClient.php(80): VK\TransportClient\Curl\CurlHttpClient->sendRequest('https://pu.vk.c...', Array)
#1 C:\OSPanel\domains\worldstories.local\vendor\vkcom\vk-php-sdk\src\VK\Client\VKApiRequest.php(105): VK\TransportClient\Curl\CurlHttpClient->upload('https://pu.vk.c...', 'photo', '/images/2023-06...')
#2 C:\OSPanel\domains\worldstories.local\components\VKApiComponent.php(25): VK\Client\VKApiRequest->upload('https://pu.vk.c...', 'photo', '/images/2023-06...')
#3 C:\OSPanel\domains\worldstories.local\models\Article.php(141): app\components\VKApiComponent->uploadPhoto('/images/2023-06...')
#4 C:\OSPanel\domains\worldstories.local\models\Article.php(244): app\models\Article->postToWallOnSave()
#5 C:\OSPanel\domains\worldstories.local\vendor\yiisoft\yii2\db\BaseActiveRecord.php(823): app\models\Article->afterSave(false, Array)
#6 C:\OSPanel\domains\worldstories.local\vendor\yiisoft\yii2\db\ActiveRecord.php(680): yii\db\BaseActiveRecord->updateInternal(NULL)
#7 C:\OSPanel\domains\worldstories.local\vendor\yiisoft\yii2\db\BaseActiveRecord.php(679): yii\db\ActiveRecord->update(true, NULL)
#8 C:\OSPanel\domains\worldstories.local\modules\admin\controllers\ArticleController.php(95): yii\db\BaseActiveRecord->save()
#9 [internal function]: app\modules\admin\controllers\ArticleController->actionUpdate('3')
#10 C:\OSPanel\domains\worldstories.local\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#11 C:\OSPanel\domains\worldstories.local\vendor\yiisoft\yii2\base\Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#12 C:\OSPanel\domains\worldstories.local\vendor\yiisoft\yii2\base\Module.php(552): yii\base\Controller->runAction('update', Array)
#13 C:\OSPanel\domains\worldstories.local\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction('admin/article/u...', Array)
#14 C:\OSPanel\domains\worldstories.local\vendor\yiisoft\yii2\base\Application.php(384): yii\web\Application->handleRequest(Object(yii\web\Request))
#15 C:\OSPanel\domains\worldstories.local\web\index.php(15): yii\base\Application->run()
#16 {main}

Код:

<?php

namespace app\components;

use VK\Client\VKApiClient;
use Yii;
use yii\helpers\Url;

class VKApiComponent extends \yii\base\Component
{
    public $accessToken;
    public $groupId;

    public function uploadPhoto($photoPath)
    {
        $vk = new VKApiClient();

        $uploadServerResponse = $vk->photos()->getWallUploadServer($this->accessToken, [
            'group_id' => $this->groupId,
        ]);
        $uploadServerUrl = $uploadServerResponse['upload_url'];

        $uploadedPhotoResponse = $vk->getRequest()->upload($uploadServerUrl, 'photo', $photoPath);
        $uploadedPhoto = $uploadedPhotoResponse['photo'];
        $uploadedPhotoServer = $uploadedPhotoResponse['server'];
        $uploadedPhotoHash = $uploadedPhotoResponse['hash'];

        $savedPhotoResponse = $vk->photos()->saveWallPhoto($this->accessToken, [
            'group_id' => $this->groupId,
            'photo' => $uploadedPhoto,
            'server' => $uploadedPhotoServer,
            'hash' => $uploadedPhotoHash,
        ]);

        $photoId = $savedPhotoResponse[0]['id'];

        return $photoId;
    }

    public function postToWall($message, $link, $photoId)
    {
        $vk = new VKApiClient();

        $params = [
            'owner_id' => '-' . $this->groupId,
            'from_group' => 1,
            'message' => $message,
            'attachments' => [
                'photo' => 'photo' . $this->groupId . '_' . $photoId,
                'link' => $link,
                ]
        ];

        $response = $vk->wall()->post($this->accessToken, $params);

        return $response['post_id'];
    }
}

Модель

    public function postToWallOnSave()
    {
        $message = $this->metaDescription; // Получаем текст статьи из модели

        $link = Url::to(['/article/single', 'slug' => $this->slug], true); // Получаем ссылку на статью
        $photoId = Yii::$app->vkApi->uploadPhoto(Yii::getAlias('@web/' . $this->image));
        Yii::$app->vkApi->postToWall($message, $link, $photoId);

    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        $this->postToWallOnSave();
    }

Но самое интересное, что ссылка и текст отправляются без проблем, опять же только с локалки, с хостинга почему то нет

aotd1 commented 1 year ago

Проверьте наличие файла, абсолютные пути, curl: (26) couldn't open file