0

I can echo the response i get just fine in the function request_callback so I thought it trivial to just save the response into an array associative_array[], however this yields just single entries, like the array gets wiped after every entry.

I make use of https://github.com/LionsAd/rolling-curl/blob/master/RollingCurl.php

<?php
# Get the all the items numbers
$url1 = "http://api.guildwars2.com/v2/commerce/listings";
$response1 = file_get_contents($url1);
$data1 = json_decode($response1, true);

#retrieve item names and link with numbers
function request_callback($response) {
    $temporary_array = json_decode($response, true);
    $associative_array[] = array('name' => $temporary_array['name'],'id' => $temporary_array['id']);
    // array[] places the new entry at end of urls stack, faster then array_push($array, new entry);
    print_r ($associative_array);
    echo "\n";
}

# Multiple curl request
require("rollingcurl.php");

for ($x=0;$x<5;$x++){   
        $itemurl1 = "http://api.guildwars2.com/v2/items/{$data1[$x]}";
        $urls[$x]= $itemurl1;  
    }   
$rc = new RollingCurl("request_callback");
$rc->window_size = 20;
foreach ($urls as $url) {
     $request = new  RollingCurlRequest ( $url ) ;
     $rc -> add ( $request ) ;
}
$rc->execute();


?>
2
  • Where does $urls come from? Can you post a var_dump of it?
    – Machavity
    Commented Oct 8, 2014 at 20:21
  • beginner mistake to try and download all the data in parallel. respect the servers
    – Ryan
    Commented Oct 8, 2014 at 20:22

2 Answers 2

0

Your array is local to your function, hence reseting on each call. Try adding global declaration and you will get what you expect (all values);

function request_callback($response) {
    global $associative_array;
    $temporary_array = json_decode($response, true);
    $associative_array[] = array('name' => $temporary_array['name'],'id' => $temporary_array['id']);
    // array[] places the new entry at end of urls stack, faster then array_push($array, new entry);
    print_r ($associative_array);
    echo "\n";
}
1
  • Thank you now the array entries get added! But I think I read while searching that global is bad practice? Commented Oct 8, 2014 at 20:35
0

I'd create your array outside of your function. It looks like you're creating a new array on every function call.

$associative_array = array();
function request_callback($response) {
        global $associative_array;
        $temporary_array = json_decode($response, true);
        $associative_array[] = array('name' => $temporary_array['name'],'id' => $temporary_array['id']);
        // array[] places the new entry at end of urls stack, faster then array_push($array, new entry);
        print_r ($associative_array);
        echo "\n";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.