【问题标题】:How to fix 'Call for undefined function' - PHP, mgp25如何修复“调用未定义函数”-PHP、mgp25
【发布时间】:2019-11-02 10:47:23
【问题描述】:

首先,我检查了几十个搜索结果,包括几篇关于此主题的 StackOverflow 文章 - 不,似乎没有人回答过它(尽管问题本身可能看起来类似于你)

总结问题:在类外调用公共函数/调用方法时,出现如下错误:'Call to undefined function...'

<?php



use InstagramAPI\Exception\ChallengeRequiredException;
use InstagramAPI\Instagram;
use InstagramAPI\Response\LoginResponse;

use InstagramAPI\Exception\RequestHeadersTooLargeException;
use InstagramAPI\Exception\ThrottledException;
use InstagramAPI\Utils;

require_once __DIR__ . '/vendor/autoload.php';


$username = "name";
$password = "password";

$ig = new \InstagramAPI\Instagram();
    try {
        $ig->login($username, $password);
    } catch (\Exception $e) {
        echo 'Something went wrong: '.$e->getMessage()."\n";
        exit(0);
    }

class RequestCollection
{
    /** @var Instagram The parent class instance we belong to. */
    public $ig;
    /**
     * Constructor.
     *
     * @param Instagram $parent The parent class instance we belong to.
     */
    public function __construct(
        $parent)
    {
        $this->ig = $parent;
    }
    /**
     * Paginate the request by given exclusion list.
     *
     * @param Request     $request     The request to paginate.
     * @param array       $excludeList Array of numerical entity IDs (ie "4021088339")
     *                                 to exclude from the response, allowing you to skip entities
     *                                 from a previous call to get more results.
     * @param string|null $rankToken   The rank token from the previous page's response.
     * @param int         $limit       Limit the number of results per page.
     *
     * @throws \InvalidArgumentException
     *
     * @return Request
     */
    protected function _paginateWithExclusion(
        Request $request,
        array $excludeList = [],
        $rankToken = null,
        $limit = 30)
    {
        if (!count($excludeList)) {
            return $request->addParam('count', (string) $limit);
        }
        if ($rankToken === null) {
            throw new \InvalidArgumentException('You must supply the rank token for the pagination.');
        }
        Utils::throwIfInvalidRankToken($rankToken);
        return $request
            ->addParam('count', (string) $limit)
            ->addParam('exclude_list', '['.implode(', ', $excludeList).']')
            ->addParam('rank_token', $rankToken);
    }
    /**
     * Paginate the request by given multi-exclusion list.
     *
     * @param Request     $request     The request to paginate.
     * @param array       $excludeList Array of grouped numerical entity IDs (ie "users" => ["4021088339"])
     *                                 to exclude from the response, allowing you to skip entities
     *                                 from a previous call to get more results.
     * @param string|null $rankToken   The rank token from the previous page's response.
     * @param int         $limit       Limit the number of results per page.
     *
     * @throws \InvalidArgumentException
     *
     * @return Request
     */
    protected function _paginateWithMultiExclusion(
        Request $request,
        array $excludeList = [],
        $rankToken = null,
        $limit = 30)
    {
        if (!count($excludeList)) {
            return $request->addParam('count', (string) $limit);
        }
        if ($rankToken === null) {
            throw new \InvalidArgumentException('You must supply the rank token for the pagination.');
        }
        Utils::throwIfInvalidRankToken($rankToken);
        $exclude = [];
        $totalCount = 0;
        foreach ($excludeList as $group => $ids) {
            $totalCount += count($ids);
            $exclude[] = "\"{$group}\":[".implode(', ', $ids).']';
        }
        return $request
            ->addParam('count', (string) $limit)
            ->addParam('exclude_list', '{'.implode(',', $exclude).'}')
            ->addParam('rank_token', $rankToken);
    }
}


class People extends RequestCollection
{


    public function getInfoByName(
        $username,
        $module = 'feed_timeline')
    {
        return $this->ig->request("users/{$username}/usernameinfo/")
            ->addParam('from_module', $module)
            ->getResponse(new Response\UserInfoResponse());
    }

}

getInfoByName("testuser")
//echo setCloseFriends(["Versaute.Witze", "Instagram"], ["leonie.kai"]);

//$ig->people->setCloseFriends($add = $addinput, $remove = $removeinput, $module = 'favorites_home_list', $source = 'audience_manager');

?>

问题在于“getInfoByName”函数——显然无法调用。 我指的 GitHub 代表是 mgp25/Instagram-API。

【问题讨论】:

  • 有人能帮忙吗?
  • 类方法与函数不同,尽管两者都是用function 关键字定义的。由于getInfoByName() 是一个类方法,因此您不能像处理函数那样独立调用它。您需要创建People 的类实例,然后调用$whatever-&gt;getInfoByName() 之类的方法。您可以在Classes and Objects 找到基本参考。
  • @ÁlvaroGonzález 谢谢 - 我以前这样做过,但是这样做时 ($test = new People();) 我遇到以下问题: PHP 致命错误:未捕获的 ArgumentCountError:函数 RequestCollection 的参数太少: :__construct(), 0 传入 ...

标签: php function undefined call instagram-api


【解决方案1】:

您必须初始化一个类的新实例。

(new People($ig))->getInfoByName('testuser')

【讨论】:

  • PHP 致命错误:未捕获的错误:在...中找不到类 'Response\UserInfoResponse' 好的,但我猜这个错误消息与原始问题无关?是吗?
  • 它没有。您必须包含必要的类文件。就这样。使用use
  • 好的,有道理!非常感谢 - 现在我还有一个“问题”。我想添加一个功能,可以将人员添加到密友列表中。必要的文档是github.com/mgp25/Instagram-API/blob/master/src/Request/…函数: public function setCloseFriends();我在线程中遇到的问题与此问题相当,因此不再相关 - 尽管我如何输入两个用户列表数组?此外,我怎样才能只添加人员或删除人员?
  • 您必须合并 2 个用户列表数组。使用 Instagram API 添加和删除人员。请阅读其文档。
猜你喜欢
  • 2020-12-01
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-03
相关资源
最近更新 更多