Argument 3 passed to GuzzleHttp\Client::request() must be of the type array, string given

时间:2016-05-11 11:31:55

标签: php facebook facebook-graph-api laravel-5.2

I'm experimenting with SammyK/LaravelFacebookSdk. Trying to run this line from example: $response = Facebook::get('/me?fields=id,name,email', 'user-access-token');

which in turn runs /var/www/vendor/facebook/php-sdk-v4/src/Facebook/HttpClients/FacebookGuzzleHttpClient.php line 61

public function send($url, $method, $body, array $headers, $timeOut)
{
    $options = [
        'headers' => $headers,
        'body' => $body,
        'timeout' => $timeOut,
        'connect_timeout' => 10,
        'verify' => __DIR__ . '/certs/DigiCertHighAssuranceEVRootCA.pem',
    ];
    $request = $this->guzzleClient->createRequest($method, $url, $options);

    try {
        $rawResponse = $this->guzzleClient->send($request);
    } catch (RequestException $e) {
        $rawResponse = $e->getResponse();

        if ($e->getPrevious() instanceof RingException || !$rawResponse instanceof ResponseInterface) {
            throw new FacebookSDKException($e->getMessage(), $e->getCode());
        }
    }

    $rawHeaders = $this->getHeadersAsString($rawResponse);
    $rawBody = $rawResponse->getBody();
    $httpStatusCode = $rawResponse->getStatusCode();

    return new GraphRawResponse($rawHeaders, $rawBody, $httpStatusCode);
}

That calls /var/www/vendor/guzzlehttp/guzzle/src/Client.php line 87

public function __call($method, $args)
{
    if (count($args) < 1) {
        throw new \InvalidArgumentException('Magic request methods require a URI and optional options array');
    }

    $uri = $args[0];
    $opts = isset($args[1]) ? $args[1] : [];

    return substr($method, -5) === 'Async'
        ? $this->requestAsync(substr($method, 0, -5), $uri, $opts)
        : $this->request($method, $uri, $opts);
}

This methond interprets input as array('method' => 'createRequest', 'uri' => 'GET'))

Changing index seems to correct the error (though other problems araise)

 $uri = $args[1];
 $opts = isset($args[2]) ? $args[2] : [];

But since it's a very bad practice to edit other packages, how should I correct this error?

3 个答案:

答案 0 :(得分:13)

我遇到了同样的问题。 更改索引对我不起作用,但我找到了一种解决方法。安装php-curl扩展通过cURL切换整个工作流程,因此问题就消失了。

答案 1 :(得分:1)

由于Facebook SDK 5.x使用guzzle版本5。 所以降级guzzle库将解决方法

$ composer require guzzlehttp/guzzle:~5.0

答案 2 :(得分:0)

使用

// Use `strncpy` to not overflow the destination buffer
std::strncpy(szBuf, ostr.str().c_str(), sizeof szBuf - 1);

// Remember that `strncpy` might not terminate the destination, so do it explicitly
szBuf[sizeof szBuf - 1] = '\0';

而不是

new \GuzzleHttp\Psr7\Request($method, $url, $headers, ..)