Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple PHP cUrl posts to same page

Tags:

post

php

curl

xml

get

So the gist is that I need to post XML data query to a gateway page to receive a XML response which O parse later, there can be anywhere from 3-60 queries to this web service, I unfortunately have to run a simple loop right now and do them one at a time. On the response side, I will only need 1 (or a max of 5) of the lines in the response, line 2 is the first line that I need containing image data. So I'd like the ability to select which lines I am reading in if at all possible.

I created a simple "Read in" function as I said out of a basic for loop, here's the code that I am currently using and would like to revise.


$part1 = 'XML Beginning'; $part2 = XML End';
$posts = array( 0 => 'SC-010052214', 1 => 'SC-000032972', 2 => 'SC-012535460', 3 => 'SC-011257289', 4 => 'SC-010134078' );


 $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, 'http://example.com/index.php');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER => 1);
  curl_setopt ($ch, CURLOPT_POST, 1);

 $count = count($posts);
 for($i=0;$i<$count;$i++) {

  curl_setopt ($ch, CURLOPT_POSTFIELDS, "payload=$part1{$posts[$i]}$part2");
  $return[] = curl_exec ($ch);

  }
 curl_close ($ch); 

print_r($return);

Restrictions: I cannot use ?post=$data0&post=$data1&post=$data3 unfortunately, so I need a better solution. Other than that, I'd like to see what kinds of improvements can be made here.

like image 711
ehime Avatar asked Nov 15 '22 05:11

ehime


1 Answers

Maybe http://php.net/manual/en/function.curl-multi-init.php helps you

like image 156
r92 Avatar answered Dec 09 '22 21:12

r92