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

CookieMonster's avatar

api call returns undefined offset:0

I am trying to make an external API call to my website. I create a createBill() method that looks like below:

public function createBill(){

        $option = array(
            'userSecretKey'=>config('toyyibpay.key'),
            'categoryCode'=>config('toyyibpay.category'),
            'billName'=>'Fuiyoh Hub',
            'billDescription'=>'Payment Credit',
            'billPriceSetting'=>0,
            'billPayorInfo'=>1,
            'billAmount'=>0,
            'billReturnUrl'=>route('toyyibpay-status'),
            'billCallbackUrl'=>route('toyyibpay-callback'),
            'billExternalReferenceNo' => 'Bill-001',
            'billTo'=>'example',
            'billEmail'=>'[email protected]',
            'billPhone'=>'01942411',
            'billSplitPayment'=>0,
            'billSplitPaymentArgs'=>'',
            'billPaymentChannel'=>'0',
            'billContentEmail'=>'Thank you for purchasing our product!',
            'billChargeToCustomer'=>2
          );  

    
          $url = 'https://dev.toyyibpay.com/index.php/api/createBill';
          
          $response = Http::asForm()->post($url,$option);
          //this line below throwing the error
          $billCode = $response[0]['BillCode'];
          dd($billCode);
         
          

          return redirect('https://dev.toyyibpay.com/'.$billCode);
    }

Route:

Route::get('/toyyibpay','ToyyibpayController@createBill')->name('toyyibpay-create');


When I tried to call the API using guzzle, I am shown an error:

Undefined offset: 0 
Upon further debugging, this line of code is the one that gives the error:

$billCode = $response[0]['BillCode'];


Based on the API docs, I am expected to get a sample result like:

[{"BillCode":"gcbhict9"}]


How do I retrieve the BillCode response?
0 likes
9 replies
SilenceBringer's avatar

@nickywan123 just dd the response

 $response = Http::asForm()->post($url,$option);
dd($response);
          //this line below throwing the error
          $billCode = $response[0]['BillCode'];
          dd($billCode);

and see what you have

CookieMonster's avatar

okay I tried :

dd($response->json());

but it's returning null.

Reinis.lucis's avatar

If the response is string you might need to decode it

$response = json_decode('[{"BillCode":"gcbhict9"}]')
$billCode = $response[0]->BillCode;
CookieMonster's avatar

The issue now is it keeps returning null when i dd($response->json());

CookieMonster's avatar

I get something like below:

Illuminate\Http\Client\Response {#314 ▼
  #response: GuzzleHttp\Psr7\Response {#349 ▼
    -reasonPhrase: "OK"
    -statusCode: 200
    -headers: array:15 [▶]
    -headerNames: array:15 [▶]
    -protocol: "1.1"
    -stream: GuzzleHttp\Psr7\Stream {#348 ▶}
  }
  #decoded: null
  +"cookies": GuzzleHttp\Cookie\CookieJar {#328 ▶}
  +"transferStats": GuzzleHttp\TransferStats {#350 ▶}
}
Reinis.lucis's avatar

Hmm, #decoded: null. Check the response body $response->body(). You should get the body of the response. Then you might need to decode it

$response = json_decode($response->body(), true, 512, JSON_THROW_ON_ERROR);
1 like
CookieMonster's avatar

I think the issue is not with the code to be honest. I tried testing it on the my postman and I get:

[KEY-DID-NOT-EXIST-OR-USER-IS-NOT-ACTIVE
]

I suspect the API key has issues even though my API key is of correct value.

Please or to participate in this conversation.