Solved

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

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.
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 …

735 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