php - foreach loop not giving required result -
i'm performing api request using php's curl library. output xml follows:
<xml> <urlnext> <list> <list1> <list2> <list3>
after getting output, i'm calling url inside urlnext
again curl , similar output , on, if keep on getting urlnext
in response call new url curl library.
but loop calling first response urlnext
not next ones. it's giving response first urlnext
doesn't go next loop. please tell me how can modify loop. here's code:
$dat1 = httpget($url); $xml2 = new simplexmlelement($dat1); foreach ($xml2 $array) { $url = $array->urlnext; $data = httpget($url); $xml2 = new simplexmlelement($data); foreach ($xml2 $array) { doing operations..... } }
the httpget
function performing curl request , returning xml expected.
<?php function getdata($url){ // $dat1=httpget($url); $ch = curl_init(); curl_setopt($ch,curlopt_url,$url); curl_setopt($ch,curlopt_returntransfer,true); // curl_setopt($ch,curlopt_header, false); curl_setopt($ch, curlopt_httpheader, array( 'header1: value1', 'header1:value2' )); $output=curl_exec($ch); curl_close($ch); //return $output; return new simplexmlelement($output); } //create main array contain xml responses of urls. $maindata = array(); //pass in url, calls getdata function, finds next url, calls self new url. forewarned--- create infinite loop function loopdata($url){ $data = getdata($url); array_push($maindata, $data); foreach($data $array){ $nexturl=$array->urlnext; loopdata($nexturl); } } //call initial url $url = "http://exampleurl.com"; loopdata($url); //print data results. print_r($maindata); ?>
one thing jumps out right away me missing "$" before "data" variable on line 6. shared variable names concerning well.
i'm not able test without knowing api you're calling, here initial stab @ creating piece of code described:
//pass in url call, returns xml element function getdata($url){ $dat1=httpget($url); return new simplexmlelement($dat1); } //create main array contain xml responses of urls. $maindata = array(); //pass in url, calls getdata function, finds next url, calls self new url. forewarned--- create infinite loop function loopdata($url){ $data = getdata($url); array_push($maindata, $data); foreach($data $array){ $nexturl=$array->urlnext; loopdata($nexturl); } } //call initial url $url = "http://www.exampleurl.com"; loopdata($url); //print data results. print_r($maindata);
Comments
Post a Comment