Solved

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

Posted on 2013-01-16
3
558 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 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

813 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

16 Experts available now in Live!

Get 1:1 Help Now