Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The viewer will learn how to dynamically set the form action using jQuery.
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 …

738 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