Jump to content

Extracting info from API Output (XMl) in a Loop


natasha_thomas

Recommended Posts

Folks,

 

I have an array having domain names & i have Coding for checking each element of this $valid array in an API and extract Information from XML.

 

BUT, looks like the this code is not working or i am possibly not able to integrate this code properely.

 

Another thing, this API allows to Check 19 Domain Max at a time, so this counter is already there in the code.

 

Here is the Code:

 

   if(empty($valid))
      return NULL;
   $rows=array();
   while(count($valid))
   {
      $this_set=array();
      $count=0;
      while($count<19 && count($valid))
      {
         $url=array_shift($valid);
         $this_set[]='item'. $count . '='. urlencode($url);
         $count++;
      }
      if(!count($this_set))
         break;
      $query='http://lightapi.majesticseo.com/api_command.php?app_api_key=API_KEY&cmd=GetIndexItemInfo&items='. $count .'&' . implode('&',$this_set);
      $content=file_get_contents($query);
      preg_match('@<DataTable.*?Headers=\"(.*?)\"@',$content,$info);
      $headers=explode('|',$info[1]);
      preg_match_all('@<Row>(.*?)</Row>@',$content,$info);
      foreach($info[1] as $line)
      {
         $data=explode('|',$line);
         $info=array_combine($headers,$data);
         $rows[]=$info;
      }
   }
   if(empty($rows))
      return NULL;
   return $rows;
}

 

 

I want to echo the output with:

 

   echo $Domain['Item'] .' '. $Domain['ACRank'] .' '. $Domain['ExtBackLinks'] .'<br />'. PHP_EOL;

 

Could someone please help with this code?

 

Regards

Natasha T

 

 

SimpleXML is a lot easier to use for parsing XML than a series of regular expressions.

What does the XML look like and what parts do you want from it?

 

You can see the output here:

 

I want to Extract ACRAnk element for Each domain name.

 

 

For a start,

// ...
$query='http://lightapi.majesticseo.com/api_command.php?app_api_key=API_KEY&cmd=GetIndexItemInfo&items='. $count .'&' . implode('&',$this_set);
$xml = new SimpleXMLElement($query, 0, true);
if ((string)$xml["Code"] == "OK") {
foreach ($xml->DataTables->DataTable as $table) {
	$headers = explode("|", (string)$table["Headers"]);
	$rows = array();
	foreach ($table->Row as $row) {
		$rows[] = array_combine($headers, explode("|", (string)$row));
	}
	print_r($rows);
}
}

Was there a specific problem you had?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.