Page 1 of 1

Warning: Invalid argument supplied for foreach()

PostPosted: 28. February 2015 18:34
by viktor6
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>

Re: Warning: Invalid argument supplied for foreach()

PostPosted: 28. February 2015 18:52
by JJ_Tagy
I recommend you suppress warnings and notices. Perhaps your data is empty?

Edit: or maybe not a type of array/hash.

Re: Warning: Invalid argument supplied for foreach()

PostPosted: 28. February 2015 19:25
by viktor6
On the hosting everything works fine. Probably full of events is not installed

Re: Warning: Invalid argument supplied for foreach()

PostPosted: 28. February 2015 21:40
by Nobbie
You should ask in Russia, where the software is coming from.

Re: Warning: Invalid argument supplied for foreach()

PostPosted: 28. February 2015 22:12
by gsmith
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>

Re: Warning: Invalid argument supplied for foreach()

PostPosted: 28. February 2015 22:28
by viktor6
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?

Re: Warning: Invalid argument supplied for foreach()

PostPosted: 01. March 2015 00:48
by gsmith
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.