Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 408
Edmund Sulzanok Avatar asked May 11 '16 11:05

Edmund Sulzanok


People also ask

How do you send a POST request on GuzzleHttp?

Sending Requests You can create a request and then send the request with the client when you're ready: use GuzzleHttp\Psr7\Request; $request = new Request('PUT', 'http://httpbin.org/put'); $response = $client->send($request, ['timeout' => 2]);

How do I get responses from GuzzleHttp?

You can check to see if a request or response has a body using the getBody() method: $response = GuzzleHttp\get('http://httpbin.org/get'); if ($response->getBody()) { echo $response->getBody(); // JSON string: { ... } } The body used in request and response objects is a GuzzleHttp\Stream\StreamInterface .

What is the use of GuzzleHttp?

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...

How do I debug guzzle request?

Debugging when using Guzzle, is quiet easy by providing the debug key in the payload: $client->request('GET', '/url, ['debug' => true]); This is quiet easy and not an issue if your are not passing any body content, using only query string to dump what's been request.


2 Answers

I've get the same problem. Changing indexes won't work for me, but I've found a workaround. Installing php-curl extension switches a whole workflow thru cURL, so the problem is vanished.

like image 122
aleks-ks Avatar answered Oct 17 '22 18:10

aleks-ks


Due to Facebook SDK 5.x use guzzle version 5. So downgrade the guzzle library will workaround

$ composer require guzzlehttp/guzzle:~5.0
like image 37
Alexander Chen Avatar answered Oct 17 '22 17:10

Alexander Chen