Warning: Invalid argument supplied for foreach()

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Warning: Invalid argument supplied for foreach()

Postby viktor6 » 28. February 2015 18:34

Code: Select all
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wg\index.php on line 14

Notice: Undefined variable: id in C:\xampp\htdocs\wg\index.php on line 23

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wg\index.php on line 26

this code does not work


Code: Select all
<?php
//Идентификатор приложения (Application_id), регистрация приложения https://ru.wargaming.net/developers/applications/
$appid = "demo";
//Тело скрипта
if(isset($_POST['nick']))
{
         $nick = $_POST['nick'];
         //Получение account_id (метод account/list)
         $urlID = "https://api.worldoftanks.ru/wot/account/list/?application_id=$appid&search=$nick&limit=1";
         $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
                 https://ru.wargaming.net/developers/api_reference/wot/account/list/     
                 */                       
        $id = $value['account_id'];
         }
                 //Получение общей статистики по боям (метод account/info)
                 $urlStat = "https://api.worldoftanks.ru/wot/account/info/?application_id=$appid&account_id=$id";
                 curl_setopt($curl, CURLOPT_URL, $urlStat);
        $resultStat = json_decode(curl_exec($curl), true);
                 foreach($resultStat['data'] as $key => $valueInfo){
                                            /* Определяем общее количество побед
                                            Значение всех параметров можно изучить в документации к методу account/info
                                            https://ru.wargaming.net/developers/api_reference/wot/account/info/           
                                            */
                                           $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>
viktor6
 
Posts: 3
Joined: 28. February 2015 18:29
Operating System: Win 7 x64

Re: Warning: Invalid argument supplied for foreach()

Postby JJ_Tagy » 28. February 2015 18:52

I recommend you suppress warnings and notices. Perhaps your data is empty?

Edit: or maybe not a type of array/hash.
JJ_Tagy
 
Posts: 678
Joined: 30. January 2012 13:44
Operating System: Windows 10 Pro x64

Re: Warning: Invalid argument supplied for foreach()

Postby viktor6 » 28. February 2015 19:25

On the hosting everything works fine. Probably full of events is not installed
viktor6
 
Posts: 3
Joined: 28. February 2015 18:29
Operating System: Win 7 x64

Re: Warning: Invalid argument supplied for foreach()

Postby Nobbie » 28. February 2015 21:40

You should ask in Russia, where the software is coming from.
Nobbie
 
Posts: 8779
Joined: 09. March 2008 13:04

Re: Warning: Invalid argument supplied for foreach()

Postby gsmith » 28. February 2015 22:12

First problem is Curl, SSL and Curl's rather lame certificate store. Just because your browser has no problem, curl certainly may. So you'll need
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); or it will return nothing.

You really only need a single loop, and then only if &limit > 1 in the first GET, if you're just going to search for one single record, no need for any loops.

Anyhow
Code: Select all
<?php
$appid = "demo";
if(isset($_POST['nick']))
{
    $nick = $_POST['nick'];
    $url = "https://api.worldoftanks.ru/wot/account/list/?application_id=$appid&search=$nick&limit=5";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $resultID = json_decode(curl_exec($curl), true);
//    print_r($resultID);
    foreach ($resultID['data'] as $key => $value) {
    $acctid = $value['account_id'];
    $nickname = $value['nickname'];
    $url = "https://api.worldoftanks.ru/wot/account/info/?application_id=$appid&account_id=$acctid";
    curl_setopt($curl, CURLOPT_URL, $url);
    $resultStat = json_decode(curl_exec($curl), true);
//    print_r($resultStat);
    $wins = $resultStat['data'][$acctid]['statistics']['all']['wins'];
    echo "<p><b>Nick:</b> $nickname <br />";
    echo "<b>AccountID:</b> $acctid <br />";
    echo "<b>Wins:</b> $wins </p>\n";
    }
    curl_close($curl);
}
?>
<p><form method="post">Nick: <input name="nick" type="text"><input type="submit" value="Check!"></form></p>
gsmith
 
Posts: 196
Joined: 29. November 2013 18:04
Location: San Diego
XAMPP version: 0.0.0
Operating System: Win XP to 2012R2/VS 6,9,11,14

Re: Warning: Invalid argument supplied for foreach()

Postby viktor6 » 28. February 2015 22:28

I repeat that in the assembly of XAMPP Problem because on another Web server, the script works, perhaps in XAMPP not included kakieto modules.
Which modules you want to include?
viktor6
 
Posts: 3
Joined: 28. February 2015 18:29
Operating System: Win 7 x64

Re: Warning: Invalid argument supplied for foreach()

Postby gsmith » 01. March 2015 00:48

I have no idea what the kakieto module is, I have basic php with php_curl extension loaded and my code works. My curl (in php) doesn't accept the ssl certificate on the api.worldoftanks.ru so the curl_exec function returns nothing.

Try adding this to yours after after curl_init();

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

and see.
gsmith
 
Posts: 196
Joined: 29. November 2013 18:04
Location: San Diego
XAMPP version: 0.0.0
Operating System: Win XP to 2012R2/VS 6,9,11,14


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 58 guests