Warning: Invalid argument supplied for foreach()
Posted by: viktor6 (93.79.167.---)
Date: February 28, 2015 06:23PM

( ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\apps\wg\index.php on line 22
Call Stack
#	Time	Memory	Function	Location
1	0.0010	259696	{main}( )	..\index.php:0

( ! ) Notice: Undefined variable: id in C:\wamp\apps\wg\index.php on line 31
Call Stack
#	Time	Memory	Function	Location
1	0.0010	259696	{main}( )	..\index.php:0

( ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\apps\wg\index.php on line 34
Call Stack
#	Time	Memory	Function	Location
1	0.0010	259696	{main}( )	..\index.php:0

( ! ) Notice: Undefined variable: clan in C:\wamp\apps\wg\index.php on line 55
Call Stack
#	Time	Memory	Function	Location
1	0.0010	259696	{main}( )	..\index.php:0

( ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\apps\wg\index.php on line 58
Call Stack
#	Time	Memory	Function	Location
1	0.0010	259696	{main}( )	..\index.php:0
this code does not work


<?php
//Идентификатор приложения (Application_id), регистрация приложения [ru.wargaming.net]
$appid = "42f70f94f175efd8c5a9a6075afaee88";
//Тело скрипта
if(isset($_POST['nick']))
{
         $nick = $_POST['nick'];
         //Получение account_id (метод account/list)
         $urlID = "[api.worldoftanks.ru];;
         $curl = curl_init();
         curl_setopt($curl, CURLOPT_URL, $urlID);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         $resultID = json_decode(curl_exec($curl), true);
         foreach($resultID['data'] as $key => $value) {
                 
                 /* Определяем account_id
                 Значение всех параметров можно изучить в документации к методу account/list
                 [ru.wargaming.net]     
                 */                        
        $id = $value['account_id'];
         }
                 //Получение общей статистики по боям (метод account/info)
                 $urlStat = "[api.worldoftanks.ru];;
                 curl_setopt($curl, CURLOPT_URL, $urlStat);
        $resultStat = json_decode(curl_exec($curl), true);
                 foreach($resultStat['data'] as $key => $valueInfo){
                                            /* Определяем общее количество побед
                                            Значение всех параметров можно изучить в документации к методу account/info
                                            [ru.wargaming.net]            
                                            */
                                           $wins = $valueInfo['statistics']['all']['wins'];
                                            curl_close($curl);
                                   echo "<b>AccountID:</b> $id <br />
                                  <b>Wins:</b> $wins";
         }
}
?>
 
<p><form method="post">Nick <input name="nick" type="text"><input type="submit" value="Check!"></form></p>

Options: ReplyQuote
Re: Warning: Invalid argument supplied for foreach()
Posted by: Otomatic (Moderator)
Date: February 28, 2015 06:45PM

Hi,

It is not a Wampserver problem. It is a PHP code problem. foreach json-decodes
Is $resultID['data'] an array?
It is an array element.

Before
foreach($resultID['data'] as $key => $value) {
insert
error_log(print_r($resultID['data'],true)) ;
Then see in wamp/logs/php_error.log what is the content of the variable.

NB: Say hello costs nothing. - Привет ничего не стоит.

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons

Options: ReplyQuote
Re: Warning: Invalid argument supplied for foreach()
Posted by: viktor6 (93.79.167.---)
Date: February 28, 2015 10:45PM

I repeat that in the assembly of WampServer Problem because on another Web server, the script works, perhaps in WampServer not included kakieto modules.
Which modules you want to include?

Options: ReplyQuote
Re: Warning: Invalid argument supplied for foreach()
Posted by: Otomatic (Moderator)
Date: March 01, 2015 10:19AM

Hello,

> another Web server, the script works,
Because, on the other server, PHP errors are hidden. If PHP indicates an error, it is that there is an error.

>kakieto modules
What is it?

And it's not worth asking the same question in all other forums like in XAMP, you will have the same answers: There is an error in your script.
Please, answer to: READ BEFORE YOU ASK A QUESTION.

I repeat:
Before
foreach($resultID['data'] as $key => $value) {
insert
error_log(print_r($resultID,true)) ;
Then see in wamp/logs/php_error.log what is the content of the array $resultID and find that $resultID ['data'] is not an array.

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons

Options: ReplyQuote
Re: Warning: Invalid argument supplied for foreach()
Posted by: viktor6 (93.79.167.---)
Date: March 01, 2015 10:41AM

<?php
//Идентификатор приложения (Application_id), регистрация приложения [ru.wargaming.net]
$appid = "demo";
//Тело скрипта
if(isset($_POST['nick']))
{
         $nick = $_POST['nick'];
         //Получение account_id (метод account/list)
         $urlID = "[api.worldoftanks.ru];;
         $curl = curl_init();
         curl_setopt($curl, CURLOPT_URL, $urlID);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         $resultID = json_decode(curl_exec($curl), true);
         foreach($resultID['data'] as $key => $value) {
		 error_log(print_r($resultID,true)) ;
                 
                 /* Определяем account_id
                 Значение всех параметров можно изучить в документации к методу account/list
                 [ru.wargaming.net]     
                 */                        
        $id = $value['account_id'];
         }
                 //Получение общей статистики по боям (метод account/info)
                 $urlStat = "[api.worldoftanks.ru];;
                 curl_setopt($curl, CURLOPT_URL, $urlStat);
        $resultStat = json_decode(curl_exec($curl), true);
                 foreach($resultStat['data'] as $key => $valueInfo){
                                            /* Определяем общее количество побед
                                            Значение всех параметров можно изучить в документации к методу account/info
                                            [ru.wargaming.net]            
                                            */
                                           $wins = $valueInfo['statistics']['all']['wins'];
                                            curl_close($curl);
                                   echo "<b>AccountID:</b> $id <br />
                                  <b>Wins:</b> $wins";
         }
}
?>
 
<p><form method="post">Nick <input name="nick" type="text"><input type="submit" value="Check!"></form></p>


php_error.log
[01-Mar-2015 10:38:41 Europe/Paris] PHP Warning:  Invalid argument supplied for foreach() in C:\wamp\apps\wg\index.php on line 14

[01-Mar-2015 10:38:41 Europe/Paris] PHP Stack trace:

[01-Mar-2015 10:38:41 Europe/Paris] PHP   1. {main}() C:\wamp\apps\wg\index.php:0

[01-Mar-2015 10:38:41 Europe/Paris] PHP Notice:  Undefined variable: id in C:\wamp\apps\wg\index.php on line 24

[01-Mar-2015 10:38:41 Europe/Paris] PHP Stack trace:

[01-Mar-2015 10:38:41 Europe/Paris] PHP   1. {main}() C:\wamp\apps\wg\index.php:0

[01-Mar-2015 10:38:41 Europe/Paris] PHP Warning:  Invalid argument supplied for foreach() in C:\wamp\apps\wg\index.php on line 27

[01-Mar-2015 10:38:41 Europe/Paris] PHP Stack trace:

[01-Mar-2015 10:38:41 Europe/Paris] PHP   1. {main}() C:\wamp\apps\wg\index.php:0

OpenServer installed it this script runs without errors



Edited 1 time(s). Last edit at 03/01/2015 10:44AM by viktor6.

Options: ReplyQuote
Re: Warning: Invalid argument supplied for foreach()
Posted by: Otomatic (Moderator)
Date: March 01, 2015 11:08AM

Hi,

$resultID['data'] IS NOT AN ARRAY. It an array index and not itself an array.

You wrote
         foreach($resultID['data'] as $key => $value) {
		 error_log(print_r($resultID,true)) ;
it is not good. I wrote before... insert
It should have been:
	error_log(print_r($resultID,true)) ;
         foreach($resultID['data'] as $key => $value) {
In order to obtain the contents of the array $resultID before the error is generated.

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons



Edited 1 time(s). Last edit at 03/01/2015 06:41PM by Otomatic.

Options: ReplyQuote


Sorry, only registered users may post in this forum.