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

me10071990's avatar

ErrorException (E_NOTICE) Undefined offset: 1

The code was working fine for API display before and today it's not working .

   $csv=explode(",",$body);

        $ApiData=$csv[1];

       foreach ($csv as $row)
       {
           $ApiData=str_getcsv($row);
       }
0 likes
38 replies
tisuchi's avatar

@me10071990 You can try this code to avoid undefined offset.

$csv=explode(",",$body);

if(count($csv) >= 2){
	$ApiData=$csv[1];
}

foreach ($csv as $row)
{
   $ApiData=str_getcsv($row);
}
Sinnbeck's avatar

Most likely you don't have multiple items in the array (no comma in the string). Check it with dd

$csv=explode(",",$body);
dd($csv, $body);
bobbybouwmann's avatar

Well, the error is pretty clear, right?

It seems that $body doesn't contain a , and therefore there is only one element in the array. So $csv[1] will never work here.

1 like
me10071990's avatar

@tisuchi thanks the error is not occuring , however in blade my value was showing before now it's showing

	API: <html>

here is blade code

          @foreach($data as $datas)

 API : {{$datas}}

@endforeach

me10071990's avatar

@sinnbeck thank you, i have csv file in which getting value and on blade was showing only one, before it was working fine now after dd

	array:1 [▼

0 => """ Please wait while you are redirected ... """ ] """

Please wait while you are redirected ...
me10071990's avatar

@bobbybouwmann thank you, i have 2 value and from this displaying one value and also trying to multiple condition so getting error.

Sinnbeck's avatar

@me10071990 Well it does indeed seem that there is only one cell in the csv data, The full string being parsed is

"Please wait while you are redirected ..."

No ,in that string at all

me10071990's avatar

@sinnbeck, so what do i do next, let me show all code , now my value was 12.11 and also trying to set if condition if value >50 then show raining, but it's not working all, thank you

here is controller

    $client = new Client();
        $response = $client->request
        ('GET', 'http://apie.com/Repository/eliv/i/latest/latest.csv');
        $statusCode = $response->getStatusCode();
        $body = $response->getBody()->getContents();
        $csv=explode(",",$body);

          $ApiData=$csv[1];


       foreach ($csv as $row)
       {
           $ApiData=str_getcsv($row);
       }

here is blade

	@foreach($data as $datas)

	   API : {{$datas}}

	@endforeach
Sinnbeck's avatar

Well it could be that the api url has changed? Notice the sentence "Please wait while you are redirected ..." That does not seem like the real data?

me10071990's avatar

it could be @sinnbeck , however i am trying to set if data is not available then it should be clear weather message and when >50 then raining message , now it's getting message like

<html> <head> <meta http-equiv="Refresh" content="1; 				 
     URL=http://172.30.185.25:8090/ssssportal/dailyFupPlanRedirection.do"> <meta http-equiv="pragma" 					 
        content="no-cache"> </head> <body> Please wait while you are redirected ... </body> </html>

on all design and it's making website bad, how can show another message if value is not there as i tried then getting error.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You can try checking the status code. If it is setup correctly in the api it should be either 302 or 301

if ($statusCode == 301 || $statusCode == 302) {
   //you were redirected and can take care of that somehow
}

You can also just try telling guzzle to follow redirects

$response = $client->request
        ('GET', 'http://apie.com/Repository/eliv/i/latest/latest.csv', ['allow_redirects' => true]);
me10071990's avatar

i followed this

$response = $client->request
    ('GET', 'http://apie.com/Repository/eliv/i/latest/latest.csv', ['allow_redirects' => true]);

however still have same message on page as error it has not been clear.

Sinnbeck's avatar

Well seems that the url isnt really working correctly. Is that the actual url you are using? If not, can you share the actual url?

Snapey's avatar

You have to allow for the CSV file containing some empty lines.

If you just assume every line will have multiple parts then you run the risk of it falling over when one day there is a blank line in there.

using dd() on the data will only show what the first line looks like and will not tell you that there are problems later in the file.

me10071990's avatar

@snapey , i did and still it's same message, i have update the dull code even

and the message

	<html> <head> <meta http-equiv="Refresh" content="1; 			 
           URL=http://172.30.185.25:8090/ssssportal/dailyFupPlanRedirection.do"> <meta http-equiv="pragma" 
           content="no-cache"> </head> <body> Please wait while you are redirected ... </body> </html>
Sinnbeck's avatar

Do you mean column? Well at least that link works.

Now are you just trying to read all of this data?

How about something like this?

$csv = array_map('str_getcsv', $body);
dd($csv);
Snapey's avatar

it might be called a CSV file but it only contains one column, so your original problem will remain.

me10071990's avatar

it's error like

array_map(): Expected parameter 2 to be an array, string given

me10071990's avatar

yes, you are right , i have two column in my real file of csv, and in the link that i updated here it has only onecolumn, so where i do modify

Sinnbeck's avatar

How about this then?

$csv = array_map('str_getcsv', explode("\n", $body);
dd($csv);
me10071990's avatar

Yes it is same in dump

array:10 [▼
0 => array:1 [▼
	0 => "<html>"
]
1 => array:1 [▼
	0 => "<head>"
]
2 => array:1 [▼
	0 => "<meta http-equiv="Refresh" content="1; 			 
    URL=http://172.30.185.25:8090/ssssportal/dailyFupPlanRedirection.do">"
]
3 => array:1 [▼
	0 => "<meta http-equiv="pragma" content="no-cache">"
 ]
4 => array:1 [▼
	0 => "</head>"
]
5 => array:1 [▼
	0 => "<body>"
]
6 => array:1 [▼
	0 => "Please wait while you are redirected ..."
]
7 => array:1 [▼
	0 => "</body>"
]
8 => array:1 [▼
   0 => "</html>"
]
9 => array:1 [▼
	0 => null
]
]
				

and without dump, when using this

	            $csv = array_map('str_getcsv', explode("\n", $body));
 	            //dd($csv);
        		   // $ApiData=$csv[1];

            	  if(count($csv) >= 2)
                      {
                      $ApiData=$csv[1];
                     }

error is str_getcsv() expects parameter 1 to be string, array given

Sinnbeck's avatar

Ok before you work on your csv parsing, you first need to figure you why you api returns that redirect page.. Once that is fixed, you can go on to working with the csv parsing.

me10071990's avatar

yes, but now how can i hide this error for temporary? can i do some condition if it is not working currently when it work fine then auto correct?

Sinnbeck's avatar

Well what status code is it returning?

dd($statusCode);
Sinnbeck's avatar

Ok maybe something like this then?

if (strpos($body, '<html>') === 0) {
    //bad response! Replace with your own logic
    abort(500, 'Bad response');
}
me10071990's avatar

@sinnbeck , I tried with this logic but still has the same

		 $csv=explode(",",$body);
        //$csv = array_map('str_getcsv', explode("\n", $body));
        //dd($csv);
        

            if (strpos($body, '<html>') === 0) 
            {
                 Stay connected...
                                
                abort(500, 'Bad response');
             }

            endif

            if(strpos($body, '<html>') >= 0)

                    {
                        $ApiData=$csv[1];

                    }

           foreach ($csv as $row)
       {
           $ApiData=str_getcsv($row);
       }

still has the same condition, any specific reason to this?

Next

Please or to participate in this conversation.