Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnk's avatar
Level 1

Interact with API (make post request to an URL with parameters)

I have a button to create an invoice after a payment using this API "https://invoicexpress.com/api/invoices/create". I'm testing with test values for now using Guzzle. I'm a beginner with API´s and I'm not understanding the process.

In the documentation says that to create a new invoice is necessary to make a POST request to the following URL "https://{account-name}.app.invoicexpress.com/invoices.xml" with the XML data of the new invoice on the request body.

I'm not understanding that part of how to send a POST request to that URL with the necessary parameters.

I have a route and method for when a button to generate an invoice is clicked:

Method:

    public function generateInvoice(){
            $client = new \GuzzleHttp\Client();
            $response = $client->request('POS', 'https://accountname.invoicexpress.com/invoices.xml');
            dd($response->getStatusCode());
        }


But how to send a Post request in the generateInvocie() to the URL of the API with the parameters? The URL doesn't have any parameter.

0 likes
3 replies
NickVahalik's avatar

You send it as the body:

// $invoiceXml = ...;
$response = $client->request('POS', 'https://accountname.invoicexpress.com/invoices.xml', ['body' => $invoiceXml] );

See the Guzzle docs.

2 likes
johnk's avatar
Level 1

Thanks, but using:

$client = new \GuzzleHttp\Client();
        $response = $client->request('POST', 'https://accountname.app.invoicexpress.com/invoices.xml', [
            'api_key' => '...',
            'body' => 'raw data'
        ]);" 

Appears "Client error: POST https://accountname.invoicexpress.com/invoices.xml resulted in a 401 Unauthorized response: Invalid API key". But the api key passed in the array is correct.

NickVahalik's avatar

You need to pass the api_key in thru the query string as per the documentation.

$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'https://accountname.app.invoicexpress.com/invoices.xml?api_key=...', [
    'body' => 'raw data'
]);

Please or to participate in this conversation.