0

how do I go on about changing query string like for example i have this curl request for an api

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

i need to request like 60 pages so which is the simple way instead of repeating the code over and over?

like request this in parallel and return the data of all

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

code here

$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);
2
  • what is the logic, relation between page / content? page is incrementing by 1, and content with 3? Commented Nov 21, 2014 at 12:51
  • i have just given an example there is no logic but the api we are requesting has something like so i was just using this as an example Commented Nov 21, 2014 at 14:22

1 Answer 1

1

Let's give a shot to this...

//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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried this code and i am getting just white page .. what might be the cause?
turn on error reporting by error_reporting(E_ALL); and ini_set('display_errors', 1); to inspect what error you you have. But I see what could be wrong. Wait, i will edit my code
try now, but add those 2 lines at the top of your code as I mentioned in my previous comment.
the first one worked with xml data only, the second works with both json and xml.. thanks now

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.