2014-10-08 41 views
0

我可以在函数request_callback中回应我刚刚好的响应,所以我认为将响应保存到数组associative_array []中很简单,但是这只会导致单个条目,例如数组在每次输入后都会被擦除。PHP-cURL添加条目到一个不起作用的函数中的数组

我利用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(); 


?> 
+0

'$ urls'从哪里来?你可以发布它的'var_dump'吗? – Machavity 2014-10-08 20:21:09

+0

初学者错误尝试并行下载所有数据。尊重服务器 – 2014-10-08 20:22:15

回答

0

你的数组是本地的你的函数,因此重置每个调用。 尝试添加全局声明,你会得到你所期望的(所有的值);

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"; 
} 
+0

现在感谢您添加数组条目!但我认为我在搜索时一直在阅读全球化是不好的做法? – Singul4r1ty 2014-10-08 20:35:26

0

我创建功能之外的阵列。看起来你正在每个函数调用中创建一个新的数组。

$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"; 
} 
相关问题