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.
Related
Im not sure why, never used to use arrays anyway till now. Well, I have this code:
$inventoryJsonUrl = 'http://steamcommunity.com/inventory/'.$steamID.'/730/2?l=english&count=5000';
$inventoryJsonGet = file_get_contents($inventoryJsonUrl);
$inventories = json_decode($inventoryJsonGet, TRUE);
foreach($inventories['success']['rgInventory'] as $key => $description)
{
echo $description['classid'];
}
And Im getting this error:
Warning: Invalid argument supplied for foreach()
I have also another trouble, how to get name/value or whatever it is thatis market on this image (it is also the Json im using: https://i.imgur.com/oaNCquW.png
If I can also ask, for any good but "simple" JSON tutorials for things that im using here?
Thanks for help.
Here you need to do this:
foreach($inventories['rgInventory'] as $key => $description)
{
echo $description['classid'];
}
Note: Steam API is a complicated API and often doesn't work the way you would like to. If you are calling more than 30 times per minute, make sure to use proxies.
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 wrote this code:
function GetIds($forum_id,$from_page,$to_page) {
$ids = array();
$dom = new DOMDocument();
libxml_use_internal_errors(true);
for($i = $from_page; $i <= $to_page; $i++){
$dom->loadHTMLFile('http://www.example.com/forumdisplay.php?page='.$i.'&f='.$forum_id);
//$xpath = new DOMXPath($dom);
//$items = $xpath->query('//ul[#id="threads"]/li[#id]');
$items = $dom->getElementById('threads')->getElementsByTagName('li');
foreach($items as $thread) {
if(($id = substr($thread->getAttribute('id'), 7)) !== false)
$ids[] = $id;
}
}
return $ids;
}
To get all the threads Ids from this forum, example of use:
$tids = GetIds("67",1,2); //Get all the Ids from page 1 to 2.
foreach($tids as $data) {
$file = fopen("threads.txt", "a+");
fwrite($file, "{$data}:");
fclose($file);
}
When i run the code i get this error:
Call to a member function getElementsByTagName() on a non-object in C:\wamp\www\eProject\functions.PHP on line 145
line 145:
$items = $dom->getElementById('threads')->getElementsByTagName('li');
What is the problem?
A direct pull of the URL is not directly returning the desired content (h/t RoyalBg). This is not an issue with your code - there is nothing malformed about the requests it produces and it should correctly process the desired content were that content being returned.
It appears that the web site you are trying to scrape no longer allows pages to be directly accessed in the way your script is attempting to access them. The site now feeds a script to the browser (some of which is obfuscated) that apparently handles loading the content.
The solution is to write additional code to deal with the scripting and correctly ask the server to send you the desired content. My gut instinct is that one purpose for the change is likely to prevent their site from being scraped. If so, having this script break is likely to be a situation that arises consistently from here on out.
It is because of the redirection.
http://www.example.com/forumdisplay.php?page=1f=67
evaluates into
http://www.example.com/forumdisplay.php?f=67
thus in the first url extracting
$items = $dom->getElementById('threads')
returns NULL instead of the relevant object.
You should be refering to a static url, also turn on warning errors for the test purpose
When got deep into the problem, I realized that the target site given probably started using Incapsula in order to prevent DDoS attacks, which stops unwanted requests.
In this cases, the target is expecting certain headers, in order to allow the request.
Unfortunately, I cannot give the exact information that the site is expecting, maybe you should examine it.
I found a discussion about what probably sites using Incapsula are expecting: http://myanimelist.net/forum/?topicid=685567&show=20
I'm attempting to use XQuery with PHP. The only implementation I'm aware of is XQuery_Lite, part of the XMLClasses library, but I'm damned if I can get it to do anything.
The demo.php page that comes with it errors. Whenever I try to execute one of the examples on there, I get:
Warning: bib is not visible from here plase use a global string for XML data in
C:\xampp\htdocs\test\xquery\xquery_lite_2\class_xquery_lite.php on line 201
Warning: Invalid argument supplied for foreach() in
C:\xampp\htdocs\test\xquery\xquery_lite_2\class_xquery_lite.php on line 690
I then created this uber-simple script (note: the code came from the read-me PDF, and bib.xml does exist):
<?php
$query = '<bib>
{
for $b in document("bib.xml")/bib/book
where $b/publisher = "Addison-Wesley" and $b/#year > 1991
return
<book year="{ $b/#year }">
{ $b/title }
</book>
}
</bib>';
$query = stripslashes($query);
include_once("class_xquery_lite.php");
$xq = new XqueryLite();
$result=$xq->evaluate_xqueryl($query);
?>
...but I'm told that the foreach on line 179 is being fed an empty array. After some digging, this appears to mean the $functions array is empty (in the main class_xquery_lite.php script).
This is all very specific so I don't anticipate anyone can help here, but you never know...
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