开发者

Looping through query links via CURL and merging result arrays in PHP

开发者 https://www.devze.com 2023-03-06 01:10 出处:网络
The following code is supposed to search for a term on twitter, loop through all the result pages and return one big array with the results from each page appended at each step.

The following code is supposed to search for a term on twitter, loop through all the result pages and return one big array with the results from each page appended at each step.

foreach($search_terms as $term){
    //populate the obj array by going through all pages

    //set up connection
    $ch = curl_init();

    // go through all pages and save in an object array
    for($j=1; $j<16;$j++){
        $url ='http://search.twitter.com/search.json?q=' . $term .'&rpp=100&page='.$j.'';
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
        $var[$j] = curl_exec($ch);
        curl_close($ch);
        $obj = array_merge((array)$obj,(array)json_decode($var[$j], true));
    }
}

It doesn't quite work though and am getting these errors:

curl_setopt(): 3 is开发者_Python百科 not a valid cURL handle resource
curl_exec(): 3 is not a valid cURL handle resource
curl_close(): 3 is not a valid cURL handle resource
...... and this is repeated all the way from 3-> 7...
curl_setopt(): 7 is not a valid cURL handle resource
curl_exec(): 7 is not a valid cURL handle resource
curl_close(): 7 is not a valid cURL handle resource


//set up connection
$ch = curl_init();
// go through all pages and save in an object array

for($j=1; $j<16;$j++){

You need the call to curl_init() inside your loop since you close it at the end of each iteration.

0

精彩评论

暂无评论...
验证码 换一张
取 消