@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);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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);
}
@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);
}
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);
please dump $csv . maybe not exists key in $csv
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.
@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}}
@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 ...@bobbybouwmann thank you, i have 2 value and from this displaying one value and also trying to multiple condition so getting error.
@abrahamghaemi , it is existing and just it's getting error, thank you.
@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
@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
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?
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.
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]);
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.
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?
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.
yes that is correct but inside company can access and now i have another link that have only one row
'https://cdn.wsform.com/wp-content/uploads/2020/06/industry.csv'
@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>
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);
it might be called a CSV file but it only contains one column, so your original problem will remain.
it's error like
array_map(): Expected parameter 2 to be an array, string given
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
How about this then?
$csv = array_map('str_getcsv', explode("\n", $body);
dd($csv);
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
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.
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?
Well what status code is it returning?
dd($statusCode);
it's returning 200
Ok maybe something like this then?
if (strpos($body, '<html>') === 0) {
//bad response! Replace with your own logic
abort(500, 'Bad response');
}
@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?
Please or to participate in this conversation.