更改查询字符串参数

时间:2014-11-21 12:48:18

标签: php curl

如何继续更改查询字符串,例如我有一个api的curl请求

www.apiurl.com/page=1&content=3

我需要请求60页,这是一种简单的方法,而不是一遍又一遍地重复代码?

喜欢并行请求并返回所有

的数据
www.apiurl.com/page=2&content=3
www.apiurl.com/page=3&content=6
www.apiurl.com/page=4&content=9

代码

$strings = array('page'=>'1',                    
                '&content'=>'3');

$postdata = http_build_query($strings);

$data = curl_init();

curl_setopt ($data, CURLOPT_URL, "www.apiurl.com/");
curl_setopt($data, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($data, CURLOPT_POSTFIELDS, $postdata);

$content = curl_exec($data);  
curl_close($data);
print_r($content);

1 个答案:

答案 0 :(得分:1)

让我们试一试......

//Create an array, what stores, what page and what conent you need
$params = array(
    array('page' => 2, 'content' => 3),
    array('page' => 3, 'content' => 6),
    array('page' => 4, 'content' => 9),
    // and so on
);

define('URL', 'www.apiurl.com/');

foreach ($params as $param) {
    $postdata = http_build_query($param, '', '&');
    $data = curl_init();
    curl_setopt($data, CURLOPT_URL, URL);
    curl_setopt($data, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($data, CURLOPT_POSTFIELDS, $postdata);
    $content = curl_exec($data);
    curl_close($data);
    print_r($content);
}