Solved

Using curl and simpleXML but can't get at the data in the result

Posted on 2013-01-16
3
577 Views
Last Modified: 2013-01-16
Hi,
I've been trying to loop through an xml response getting it with curl and simpleXML. I can't figure out how to get at the values in the simpleXML object. Can you give me an example how to access them ie: a foreach loop perhaps? Nothing I'm trying seems to work.

<?php

//URL of targeted site
$url = "http://thedomaintowebservice.com";

$command = years;

$fields = array(
            'command' => urlencode($command),
            'year' => urlencode($year),
            'make' => urlencode($make),
            'trim' => urlencode($trim),
            'style' => urlencode($style),
            'account' => urlencode('myusername'),
            'key' => urlencode('mykey')
        );


//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER , 1 );

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);


$xml = new SimpleXMLElement($result);

?>

Open in new window


This is the xml being returned:

<?xml version="1.0"?>
<CBB>
<Response>
<DataVersion>20130114</DataVersion>
<Years>
<Year VINYear="D">2013</Year>
<Year VINYear="C">2012</Year>
<Year VINYear="B">2011</Year>
<Year VINYear="A">2010</Year>
<Year VINYear="9">2009</Year>
<Year VINYear="8">2008</Year>
<Year VINYear="7">2007</Year>
<Year VINYear="6">2006</Year>
<Year VINYear="5">2005</Year>
<Year VINYear="4">2004</Year>
<Year VINYear="3">2003</Year>
<Year VINYear="2">2002</Year>
<Year VINYear="1">2001</Year>
<Year VINYear="Y">2000</Year>
</Years>
</Response>
</CBB>

Open in new window


I tried this and it returns just the first year: 2013.

$xml = new SimpleXMLElement($result);

foreach ($xml->Response->Years as $year) {
    echo '<p>'.$year->Year.'</p>';
}

 I can't get my head around what I'm looping.
0
Comment
Question by:tjyoung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 38785433
Try this:

foreach ($xml->Response->Years->Year as $year) {
    echo '<p>'.$year.'</p>';
}
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 38785845
Thanks, I must have tried 100 different combinations except that
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38785855
You're welcome, I tried a couple of others before that.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this article, we’ll look at how to deploy ProxySQL.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question