php parser xml with curl [duplicate] php parser xml with curl [duplicate] curl curl

php parser xml with curl [duplicate]


CURL has nothing to do with XML, it's simply a delivery mechanism that retrieves XML for you. It's like saying milk tastes different because one delivery driver uses a car and one uses a truck.

To process XML in PHP, use DOM or SimpleXML

$dom = new DOMDocument();$dom->loadXML($xml); // where $xml is the result from curl... do whatever you want with the DOM object here ...


You can just load the result into SimpleXML and loop over the data. Here's an example:

define("EMAIL_ADDRESS", "youlichika@hotmail.com");$ch = curl_init();$cv = curl_version();$user_agent = "curl ${cv['version']} (${cv['host']}) libcurl/${cv['version']} ${cv['ssl_version']} zlib/${cv['libz_version']} <" . EMAIL_ADDRESS . ">"; curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");curl_setopt($ch, CURLOPT_ENCODING, "deflate, gzip, identity");curl_setopt($ch, CURLOPT_HEADER, FALSE);curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);curl_setopt($ch, CURLOPT_HTTPGET, TRUE);curl_setopt($ch, CURLOPT_URL, "http://en.wikipedia.org/w/api.php?action=opensearch&search=tokyo&format=xml&limit=2");$xml_data = curl_exec($ch);curl_close($ch);// Make sure we actually have something to parseif($xml_data) {  $parser = simplexml_load_string($xml_data);  foreach($parser->Section as $section) {    foreach($section->Item as $item) {      echo "{$item->Text}\n\n{$item->Description}\n--------------------\n";    }  }}

Here's some sample output:

$ php test.phpTokyo, officially , is one of the 47 prefectures of Japan.--------------------Tokyo Broadcasting System(), TBS Holdings, Inc. or TBSHD, is a stockholding company in Tokyo, Japan. It is a parent company of a television network named and radio network named .--------------------