i am having a code which parses the xml text which is obtained from google search
it used to working fine before , I think after updating my version infos it not working fine
what might be the problem i am getting the following error
Fatal error: Call to undefined method SimpleXMLElement::child_nodes() in /home/search.php in line 70
Please let me know how can i solve this problem
This code is used in smarty
And it gets the response string from curl.
SimpleXMLElement does not have a method called child_nodes. Were you looking for the children method?
okay many thanx for your answers .
i got solved my problem by using the domxml-php4-to-php5.php file i have just uploaded this file and included that file name in my file . And it got worked .
I got that file from this link : http://alexandre.alapetite.fr/doc-alex/domxml-php4-php5/
:)
use the following notation to iterate thru your xml (change 'myfile.xml' and 'tagName'):
<?php
include('simple_html_dom.php');
if (file_exists('myfile.xml')) {
$xml = simplexml_load_file('myfile.xml');
print_r($xml);
foreach( $xml->children() AS $child ) {
$name = $child->getName();
if ($name == 'tagName') {
foreach( $child->children() AS $grandchild ) {
// DO SOMETHING
}
}
}
}
?>
there are other more elegant ways to achieve this, but this is a simple beginner's way to do it. for technical info: http://us2.php.net/manual/en/class.simplexmlelement.php
Related
I am fairly new to php, and I have written code to work with the amazon API. When I request information from the API, I receive it, but am unable to sort through the XML. Here is the error:
Fatal error: Call to a member function children() on null in J:\XAMPP\htdocs\Phillip\src\MarketplaceWebServiceProducts\Samples\csv_prep.php on line 117
Here is the code:
if(is_array($xmlFiles)){
foreach($xmlFiles as $xmlFile){
$xml = simplexml_load_file($xmlFile);
foreach($xml->GetMatchingProductForIdResult as $items) {
//Line 117 ->
if(isset($items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount) !== False) {
$amount = $items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount
}else{
$amount = '0.00';
}
}
}
}
The tag in the XML that I am trying to get the value of looks like this:
<ns2:amount>9.99</ns2:amount>
It is in the same place as it says in the code. I only have this problem with large files and I am not sure what is happening. If someone could help, I would greatly appreciate it. Thanks in advance!
I'm not certain, but based on the error message, it looks like somewhere in the $items->Products->Product->AttributeSets hierarchy something you're specifying does not exist.
Do tests to see if $items or $items->Products or $items->Products->Product or $items->Products->Product->AttributeSets and whichever one fails, print out that fact and call exit;.
You could do a print_r on $items to help as well.
I have this problem.
I used this sentence to get post info in CodeIgniter 1.7.2 and it works fine
function function1(){
$input_data = json_decode(trim(file_get_contents('php://input')), true);
$info = str_replace( '"', '', json_encode($input_data['info']));
}
My input json is this:
{
"info":"hello!"
}
But when I used these same lines in CodeIgniter 2.1.3 it doesn't work. I used echo $info but my answer is null. Anyone can help me? Where is the mistake?
This behavior is not CI related. It's most PHP concepts.
Your mistake is probably that you're trying to access something that doesn't exist.
What is the point to use file_get_contents('php://input')? Can't you just use $this->input->post(), or the uploadclass? (in CI context)?
Anyway, check what do you get in file_get_contents(), var_dumping it. I'm pretty sure that your NULL comes from $input_data assignment statment, so you're fetching the JSON in the wrong way at first.
I'm using a Controller as a WebService and It works fine in a previous work. So, I tested with this
function test(){
$m = $this->input->post('key');
echo $m;
}
and I'm sending a POST JSON data as follow:
{
'key':'hello!"'
}
and now I receive nothing. Some else?
I am trying to find the game someone is playing on twitch by using the api. I have setup the json_decode and it shows all of the content from the api. However whenever I try to print_r the game I get an error.
The error:
Notice: Undefined property: stdClass::$game in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\Portfolio -- Website\twitchstreaminfo\streaminfo.php on line31
PHP code:
$streamer = $_POST['username'];
$apiurl = "https://api.twitch.tv/kraken/streams/" . $streamer;
$apicontent = file_get_contents($apiurl);
$streamerinfo = json_decode($apicontent);
print_r($streamerinfo->game);
Try just doing the following first and verify the result:
print_r( $streamerinfo );
From what I can see with the API, the following should work:
print_r( $streamerinfo->stream->game );
Your error is saying that the propery of "$game" does not exist on the object "$streamerinfo". As suggested above, try priting the "$streamerinfo" to varify that it is valid. Another thing you can do to prevent this is to add the following :
if (isset($streamerinfo->game) {
print_r($streamerinfo->game);
}
That code will prevent this error, but not fix the problem. I suggest this as a final solution to help you solve the problem
if (isset($streamerinfo->game) {
print_r($streamerinfo->game);
} else {
print_r($streamerinfo);
}
This will keep your code from breaking in the way that it is now. But, it will also print "$streamerinfo" if it fails. This way you can see why it failed.
I am using a very cool php library(whatever it is called) called SimplePie. I am using the latest version.
I have this code:
$url = 'http://www.seobook.com/feeds.shtml';
$SimplePieFeed->set_feed_url($url);
$SimplePieFeed->force_feed(true);
$SimplePieFeed->enable_order_by_date(true);
$success = $SimplePieFeed->init();
if( !$SimplePieFeed->error() ) {
foreach( $SimplePieFeed->get_items() as $item ) {
......
}
} else {
print_r( $SimplePieFeed->error() );
}
Why is it that when I run this code I'm getting this kind of error:
This XML document is invalid, likely due to invalid characters. XML error: not well-formed (invalid token) at line 8, column 76
I try to run this one on Simplepie's demo and everything is going well. Why is it that when I run it on my end i'm having that kind of error? Is it because of a cache? I noticed that Simplepie is storing feeds in a cache. I have tried $SimplePieFeed->enable_cache(false); but still i'm getting that error. I'm not even sure if that's related to that kind of error. LOL!
Your help would be greatly appreciated and rewarded! :Thank you very much!
Simple there is problem in your xml file you should remake the .xml file,if you are using wordpress simple use plugin called google site maps its pretty good.
If you using some thing else like php, or html base site you should make valid xml document maker like some listed here,
xml-sitemaps.com
xmlgrid.net (editor viewer)
web-site-map.com
May it help you little in your case.
I want to use the servicesidekick API using php, as per their documentation at here: http://www.servicesidekick.com/help/api
They are suggesting to use curl for this. I am suppose to get xml file in response.
Below is my code:
exec('command',$data);
when I print the $data then its giving each line of xml in each element of Array. But i want it to be accessed as the XML file.
You can use the SimpleXML functions to make things easy. Here is a very basic example that prints out some comma-deimited fields:
<?php
$token = "123xxx";
$account = "myaccount";
$url = "https://{$token}:{$token}#{$account}.servicesidekick.com/jobs.xml";
$xml = simplexml_load_string(file_get_contents($url));
foreach ($xml->children() as $job) {
echo "{$job->id},{$job->name},{$job->{"job-number"}},{$job->{"balance-remaining"}} \n";
}
?>
It's pretty easy to get access to all of the fields. This example just demonstrates jobs, but all of the entities can be called the same way. Good luck!
Please read the docs of the PHP curl bindings.
There is no function curl() which does lots of magic like you apparently assume in your code.