invoiceninja / sdk-php

PHP wrapper for Invoice Ninja's REST API
https://www.invoiceninja.com
83 stars 41 forks source link

Retrieve client by E-Mail #34

Closed dgram closed 6 years ago

dgram commented 6 years ago

Hi,

i've tried to retrieve an client by

Client::find('email=test@test.com'); Client::find('test@test.com');

always an exception, then i look into the code and see that the find method ends with an / instead of an ? before the param.

see here https://github.com/invoiceninja/sdk-php/blob/master/src/InvoiceNinja/Models/AbstractModel.php#L35

is this an error or do i something wrong?

marcoboers commented 6 years ago

The find method is used to retrieve a model, a client in this case, by it's id. It is not a search function nor does it accept other parameters.

marcoboers commented 6 years ago

I have inspected the API and the index route does seem to support to find clients by email. The SDK does however need a minor update to support it.

@dgram if you'd like, I might be able to test and send some modifications to make it work tomorrow.

dgram commented 6 years ago

@marcoboers94 perfect thanks for your fast answer

scryba commented 6 years ago

I wrote this custom function in my laravel project to solve that.

it uses https://github.com/guzzle/guzzle

public function findClientByEmail($email)
    {

            $url = env('INVOICENINJA_URL')."/clients";
            $token = env('INVOICENINJA_API_TOKEN');

            $client = new GuzzleHttp\Client();
            $response = $client->request('GET', $url, [
                'headers' => [
                    'X-Ninja-Token' => $token,
                ],
                'query' => [
                    'email' => $email,
                ]
            ]);
            $json_response = $response->getBody();
            $responseDecoded = json_decode($json_response, true);

            if(!empty($responseDecoded['data']))
            {
                return $responseDecoded;
            }
            else
            {
                return false;
            }

    }

you can for example call this function.

var_dump(findClientByEmail("demo@example.org"));

dgram commented 6 years ago

perfect, thank you for your help