Taking only 4 items from 1059 instead of all

Asked

Viewed 63 times

1

I was able to solve my date problem. Currently my problem is to catch all users for some reason my Json is giving only 4 items of 1059 and I want to catch all.

<?php 
function buscaUser($user)
{
$opts = [
        'http' => [
                'method' => 'GET',
                'header' => [
                        'User-Agent: PHP'
                ]
        ]
];
$user=  $_GET['username'];
$context = stream_context_create($opts);
$content = file_get_contents('https://api.github.com/search/users?q='.$user, false, $context); 
$json = json_decode($content,true);
$jsoncount = count($json);
echo $jsoncount."<br>";
for ($numItem = 0 ; $numItem <= $jsoncount; $numItem++){

echo "<img class='img-thumbnail' width='100px' height='100px' src=".$json['items'][$numItem]['avatar_url']."><br>";
echo $json['items'][$numItem]['login']."<br>";
echo "<a href=".$json['items'][$numItem]['html_url'].">Repositorio</a><br>";
}
}
?>
  • Without the foreach you can? that code ai generates some warnings

2 answers

3


items is a array of arrays, you must specify the index, I made some small changes to your code, see:

function buscaUser($user) {
    $opts = [ 'http' => [
              'method' => 'GET',
              'header' => [ 'User-Agent: PHP' ]
            ]];

    $context = stream_context_create($opts);
    $content = file_get_contents("https://api.github.com/search/users?q=$user", false, $context); 

    $json = json_decode($content, true);

    if ($json['total_count'] > 0) { // Se há dados
        foreach ($json['items']['0'] as $item) {
            echo $item . "<br>";
        }

        // Para acessar um item especifico
        echo $json['items']['0']['login'] . "<br>";
        echo $json['items']['0']['url'] . "<br>";
    }
}

$user = isset($_GET['username']) ? $_GET['username'] : 'NaixSpirit';
buscaUser($user);

To traverse items, you can do so:

// ...
$json = json_decode($content, true);
$json_count = count($json['items']);

if ($json_count > 0) {
    for ($indice = 0; $indice < $json_count; $indice++) {
        $login = $json['items'][$indice]['login'];
        $url =   $json['items'][$indice]['url'];

        echo "$login, $url <br>";
    }
}
  • for ($numItem = 0 ; $numItem <= $jsoncount; $numItem++){ echo "<img class='img-thumbnail' width='100px' height='100px' src=". $json['items'][$numItem]['avatar_url']." ><br>"; echo $json['items'][$numItem]['login']." <br>"; echo "<a href=". $json['items'][$numItem]['html_url']." >Repositorio</a><br>"; } .

  • 1

    @Rodrigolessa I think it’s because $jsoncount has value 4, confirm that this is right: echo $jsoncount . "<br>";. Something else, echo $json['items']['0']['login'] doesn’t solve?

  • I don’t know why the value was 4 but I changed Jsoncount to $jsoncount = Count($json['items']); and this returned me 30 values. I believe that on account of the api divide into pages. "user_search_url": "https://api.github.com/search/users?q={query}{&page,per_page,Sort,order}" I just have no idea how to integrate this into the loop.

  • @Rodrigolessa 30 is value correct? another thing, what is the URL you are using? because https://api.github.com/search/users?q=NaixSpirit returns only one arrayinformation here for me.

  • This array was just one example, there are several others inside. In this case I used the api to get some name ($user in the case) from https://api.github.com/search/users?q= , it returns 30 items, one of them was Naix. Now what I need is to add the pagination of the API itself as https://api.github.com/search/users?q=spirit&page=2 where it returns 30 more and so on.

1

Instead of iterating the integer array(json), specify which key you want (items).

The code below iterates the keys total_count, incomplete_results that have scaler values (simple) so a Warning is generated because the key login does not exist, the following is items as $json['items']['items'] or $json['items']['items']['login'] there are no more this generates more warnings.

To solve this just specify the 'right starting point' of the array.

Change:

foreach ($json as $json) {
   echo $json['items']['login'];
}

For:

foreach ($json['items'][0] as $item) {
    echo $item .'<br>';
}
  • Left error "Array to string Conversion". Had even made a solution with a for for ($numItem = 0 ; $numItem <= $jsoncount; $numItem++){ echo $json['items'][$numItem]['login']." <br>"; } but this only brought 4 json objects despite displaying correctly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.