I am working with PHP in my Symfony project. I need to use the data, from my 1st API call, in my 2nd API call. Furthermore, I am using the Guzzle HTTP client. Everything is working properly, except when I use the json_decode function on the 2nd API call's response. The output is always the response of the 1st API call.

#first api call
    $response = $baseApiCall->sendRequest(
        'GET',
        'bbcpurchaseOrders?$expand=bbcpurchaseOrderLines&$filter=lastModifiedDateTime gt '.$startDate           
    );
    $combinedResults = [];
    $data = json_decode($response->getBody()->getContents(),true)['value'];
    #-------------------------------------
    foreach ($data as $order) {
        $orderNumber = $order['number']; // Replace with actual key for order number
       
        if (isset($order['bbcpurchaseOrderLines']) && is_array($order['bbcpurchaseOrderLines'])) {
            foreach ($order['bbcpurchaseOrderLines'] as $lineItem) {
                $lineObjectNumber = $lineItem['lineObjectNumber'];
                
                $receiptsApiUrl = "<api URL I am using>/purchaseReceipts?\$filter=orderNumber eq '$orderNumber'&\$expand=purchaseReceiptLines(\$filter=lineObjectNumber eq '$lineObjectNumber')";

                try {
                    $receiptsResponse = $baseApiCall->sendRequest('GET', $receiptsApiUrl);

                    if ($receiptsResponse->getStatusCode() == 200) {
                        $receiptsData = json_decode($receiptsResponse->getBody()->getContents(), true);

I can see the data in my $receiptsApiUrl is indeed the response from my second api call, but for some reason when I put it through the json_decode function, I get the results from the 1st API call. Is there a way to force the json_decode function to clear the cached data? Or another step that I can do to make sure the 2nd API response is what is in the result?

1

There are 1 best solutions below

0
Kram_Koorbse On

A solution that is working For some reason the issue is with my baseApiCall object. So I created two distinct objects, one for each api request.

$baseApiCall = $this->baseApiCall;
$baseApiCall2 = $this->baseApiCall2;

However, this did not fully fix my issue. So, I also had to add a line of code to clear the URL for the newly created $baseApiCall2 object.

$this->baseApiCall2->setFullUrl('');
$receiptsResponse = $baseApiCall2->sendRequest('GET', $receiptsApiUrl);

Now my code is working and I am getting distinct api results for each of the baseApiCall objects.