-3

I need help to create a loop with different value for AID with below coding format

<?php

$curl = curl_init();

curl_setopt_array($curl, array(

  CURLOPT_URL => "https://api.max.com/core/v2/leads/getLeadData.do?AID=?????&api_secret=XG123CB&api_key=XGB3928?", CURLOPT_RETURNTRANSFER => true,   CURLOPT_ENCODING => "",   CURLOPT_MAXREDIRS => 10,   CURLOPT_TIMEOUT => 30,   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,   CURLOPT_CUSTOMREQUEST
=> "GET",   CURLOPT_POSTFIELDS => "{}",   

    ));

$response = curl_exec($curl); $err = curl_error($curl);

curl_close($curl);

if ($err) {   echo "cURL Error #:" . $err; } else {   echo $response; }

   } ?>
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • What are you expecting the result to be? What have you tried to get to where you want to be? Do you have any clear information about what exactly is going on here? – IncredibleHat Dec 11 '17 at 21:38
  • i wast trying to get information from a curlop url site but i can only do one a time when i hardcoded the AID. I want to make the AID variable with array and loop it. – Reisling Dec 11 '17 at 21:43
  • You only need to set the curlopts once, then you can just change the URL and re-run curl_exec(). – DDeMartini Dec 11 '17 at 22:30

1 Answers1

0

Set up your list of AIDS in the AID array, set your CURLOPTS just once. Change the url for each of your AIDS and then do something with the results as you like.

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_ENCODING => "",
   CURLOPT_MAXREDIRS => 10,
   CURLOPT_TIMEOUT => 30,
   CURLOPT_CUSTOMREQUEST => "GET",
   CURLOPT_POSTFIELDS => "{}",
));

$AIDs = array(  ...  your list of AIDS);

foreach($AIDS as $id) {
   $url = sprintf("https://api.max.com/core/v2/leads/getLeadData.do?AID=%s&api_secret=XG123CB&api_key=XGB3928?",$id);
   curl_setopt($curl,CURLOPT_URL,$url);
   $response = curl_exec($curl); $err = curl_error($curl);

   if ($err) {
      echo "cURL Error #:" . $err; 
   } else {
      # do something this the response   
      echo $response;

      sleep(0);  # or sleep longer to not piss off the website you're harvesting 
   }
}

curl_close($curl);

} ?>

Now, you might need to toss 'sleep' in there to keep from abusing the website you're hitting too.

DDeMartini
  • 339
  • 1
  • 6