Solved

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

Posted on 2013-01-16
3
553 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
  • 2
3 Comments
 
LVL 82

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 82

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now