LAravel Http facade not working as expected while trying to consume a CiviCRM API

33 Views Asked by At

Having a beast of a time getting the Http facade to pass params to CiviCRM API correctly.
I can get it to communicate properly if I use Guzzle directly (which is probably the sensible thing to do, but I am committed now) What am I doing wrong? Is there a way to tell Http::post to NOT post using Json (so I can manually encode it?) ?

More details in the comments int he code.

My question is: "Why is the Http facade not working as expected?"

public function makeRequest($entity, $action, $params = [])
{
    echo '<pre>';

    $params = [
        'select' => ['id'],
        'limit' => 2,
    ];

    /** example taken from CIVICRM API expolrer, but not working
     * it returns all results, not just the first 2
     * note, it is using a get request, not a post request
     * User-Agent: GuzzleHttp/7
     * Content-Type: application/x-www-form-urlencoded
     * Content-Length: 60
     */
    $client = new \GuzzleHttp\Client([
        'base_uri' => $this->endpoint,
        'headers' => ['X-Civi-Auth' => 'Bearer ' . $this->apiKey],
        'debug' => true,
    ]);
    $response = $client->get('LocationType/get', [
        'form_params' => ['params' => json_encode($params)],
    ]);
    $locationTypes = json_decode((string) $response->getBody(), TRUE);

    dump($locationTypes); // dumps 5 results (all of them)



    /**
     * This examples uses the post method and returns the first 2 results correctly
     * It's user agent, content type and length are the same as above
     * User-Agent: GuzzleHttp/7
     * Content-Type: application/x-www-form-urlencoded
     * Content-Length: 60
     */
    $client = new \GuzzleHttp\Client([
        'base_uri' => $this->endpoint,
        'headers' => ['X-Civi-Auth' => 'Bearer ' . $this->apiKey],
        'debug' => true,
    ]);
    $response = $client->post('LocationType/get', [
        'form_params' => ['params' => json_encode($params)],
    ]);
    $locationTypes = json_decode((string) $response->getBody(), TRUE);

    dump($locationTypes); // shows the correct number of results

    /**
     * This is a complete mystery it always returns 5 results
     * regardless of the way parms are passed in
     * it always has a shorter content length of 27
     * content type does not matter if set to json or form
     */
    $response = Http::dump()->withHeaders([
        'X-Civi-Auth' => 'Bearer ' . $this->apiKey,
        'Content-Type' => 'application/x-www-form-urlencoded',
        'debug' => true,
    ])->post($this->endpoint . 'LocationType/get', [
        // does not work, returns 5 results
        // 'form_params' => ['params' => json_encode($params)],

        // I think this should be correct, but it returns 5 results
        'params' => json_encode($params),

        // I am just guessing at this point....... 5 results
        // 'params' => $params,  // nope
        // $params, // nope
        // 'json' => $params, // nope
        // 'limit' => 1, // nope
        // 'select' => ['id'], // nope
    ]);

    dd($response->object());
}
0

There are 0 best solutions below