Jump to content

Recommended Posts

I have a Curl call that returns a SimpleXMLElement Object  which works.

I am struggling to get the values into useable form say php variables or array.

Any thoughts welcome

 

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, 'http://api.zoopla.co.uk/api/v1/zed_index?area=TW14TR&output_type=outcode&api_key=$Key');
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response= curl_exec($curl);
curl_close($curl);

$xml= new SimpleXMLElement($curl_response);

echo "

";print_r($xml);echo "

";

 

Outputs

SimpleXMLElement Object
(
[area_name] => Hawkesley Close, Twickenham TW1
[area_url] => http://www.zoopla.co.uk/home-values/twickenham/hawkesley-close
[bounding_box] => SimpleXMLElement Object
(
[latitude_max] => 51.43454
[latitude_min] => 51.43454
[longitude_max] => -0.33085
[longitude_min] => -0.33085
)

[country] => England
[county] => London
[latitude] => 51.43454
[longitude] => -0.33085
[postcode] => TW1
[street] => Hawkesley Close
[town] => Twickenham
[zed_index] => 783551
[zed_index_1year] => 718786
[zed_index_2year] => 667084
[zed_index_3month] => 788213
[zed_index_3year] => 648497
[zed_index_4year] => 620967
[zed_index_5year] => 613178
[zed_index_6month] => 800297
)

Give this a try

foreach ($xml as $key=>$value) {
  $array{$key} = $value;
}

That should give you an associative array where the xml->node is the key and its values the value. Note: your creating XML.  You could also check out asXML()

Sorry for the second post, wanted to edit the original.  I'm opening an XML locally, but how I'm accessing the array elements are what you're looking for. 

$xml_path = simplexml_load_file("path/to/mylocal.xml");
foreach($xml_path as $key=>$value) {
	$array{$key} = $value;
}
echo "<pre>";
print_r($array);
echo "</pre>";

echo "this is the headline element: ".$array['blog']->thead;

/* output
Array
(
    [blog] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id] => 1
                )

            [tdate] => 10th Jan 2014
            [thead] => From Marketing
            [tpost] => New marketing rules for our network. 
        )

)
this is the headline element: From Marketing
*/
Edited by rwhite35

Hi,

Thanks for that.

I have add the first post code and get the first key and value, but I need all the keys and values.

I would also need to use the names of the fields as the php variables. eg $country= ..... [country] => England

Output below.

I dont quite follow your second post yet?

 

foreach ($xml as $key=>$value) {
$array{$key} = $value;
}
echo "

";print_r($value);echo "

";

SimpleXMLElement Object
(
[0] => 800297
)

can you attach the XML file?  Something in the XML is formed different then what my answer is expecting.

 

Hi,

Thanks for that.

I have add the first post code and get the first key and value, but I need all the keys and values.

I would also need to use the names of the fields as the php variables. eg $country= ..... [country] => England

Output below.

I dont quite follow your second post yet?

 

foreach ($xml as $key=>$value) {
$array{$key} = $value;
}
echo "

";print_r($value);echo "

";

SimpleXMLElement Object
(
[0] => 800297
)

Why not assign the xml object to an array.  I've updated my code using your CURL, so in affect CURL should be returning the same thing for either call.  

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, 'http://example.com/fake/path/to/my.xml');
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response= curl_exec($curl); //returns string
curl_close($curl);

var_dump($curl_response);

$xml= new SimpleXMLElement($curl_response); //converts string to xml object
$array['blog'] = $xml->blog; //assign object to array

var_dump($array);

echo "The headline is: ".$array['blog']->thead; //access individual elements


//Outputs


//var_dump array
array (size=1)
'blog' =>
object(SimpleXMLElement)[2]
public '@attributes' =>
array (size=1)
'id' => string '1' (length=1)
public 'tdate' => string '10th Jan 2014' (length=13)
public 'thead' => string 'From Marketing' (length=14)
public 'tpost' => string 'You are currently ...' (length=14)

The headline is: From Marketing
Edited by rwhite35

To covert the SimpleXML Object into an array you should be able to typecase. Example

$xml = new SimpleXMLElement($curl_response);
$array = (array) $xml;
print_r($array);

Why does it need to be an array/variables? Are you not familiar with objects Lassie?

Hi,

Thanks both for that.

Using Ch)cu3r's input, I get

the following.below which is the same as the data array that is returned. So Iguess my question is how to get those values.

I am not that familiar with objects that is correct. I thought if I got the values into an array I could cylce through them, put them into php variables which I could selectively store in a database.

Would there be an easier way?

No sure if I should post a new topic but additionally I need to put the variable data for the curl request into the url , something like this, but it returns an error. Perhaps someone could advise me on the correct action.

 

$key= "......";//mixed string
$post_code= "TW14TR";

$url = $url= "http://api.zoopla.co.uk/api/v1/zed_index?area=' . $post_code . '&output_type=outcode&api_key=' .  $Key . ';

Result form amended code

 

(
[area_name] => Hawkesley Close, Twickenham TW1
[area_url] => http://www.zoopla.co.uk/home-values/twickenham/hawkesley-close
[bounding_box] => SimpleXMLElement Object
(
[latitude_max] => 51.43454
[latitude_min] => 51.43454
[longitude_max] => -0.33085
[longitude_min] => -0.33085
)

[country] => England
[county] => London
[latitude] => 51.43454
[longitude] => -0.33085
[postcode] => TW1
[street] => Hawkesley Close
[town] => Twickenham
[zed_index] => 783551
[zed_index_1year] => 743182
[zed_index_2year] => 684976
[zed_index_3month] => 787538
[zed_index_3year] => 648894
[zed_index_4year] => 636067
[zed_index_5year] => 631426
[zed_index_6month] => 789645
)

 

So Iguess my question is how to get those values.

The text between the square brackets are called the array keys. The text after the => is the value assigned to that key. To get a value from an array you use this syntax $array['key']

So if you want to get the area_code value you would use $xml['area_name']

 

With objects the text in the square brakes is the property name. The text after the => is the value assigned to that property. To get the value from an object you use this syntax $object->property

So if you want to get the area_name value you would use $xml->area_name;

 

Note the value assigned to the bounding_box key is an object. So to get those values you use this syntax $array['key']->property. For example to get the latitude_max value you would use this syntax $xml['bounding_box']->latitude_max. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.