simple_html_dom.php parser not working - php

I downloaded the simple_html_dom.php file and uploaded it to my web server and I immediately tested it with a simple script, but it won't work, or at least I suppose it doesn't since it doesn't output anything.
Here's the script:
<?php
require('simple_html_dom.php');
// Retrieve the DOM from a given URL
$html = file_get_html('http://davidwalsh.name/');
// Find all "A" tags and print their HREFs
foreach($html->find('a') as $e) {
echo $e->innertext . '<br>';
}
?>
I got the script from this site http://davidwalsh.name/php-notifications and the same was presented on other sites so I don't understand why it won't output anything.
My guess is that the script isn't able to retrieve any data from the other website, something like the problem I came across here: Retrieving cross-domain data php.
If it were like so, could there be any way to avoid the problem?
In BIno Carlos' answer to the question I linked before he stated "it's not really a cross domain problem because you are loading the data from the server not the browser", so could there be for example a way to load the data from the browser?
So, as suggested by user868766 in his answer, i tried the two ini_set methods which didn't report any errors, so it would seem like the script works apparently just fine. When I tried the print_r() method on $html it outputted the below:
simple_html_dom Object ( [root] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => [_] => Array ( [0] => -1 [1] => 1 ) [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [nodes] => Array ( [0] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => [_] => Array ( [0] => -1 [1] => 1 ) [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) ) [callback] => [lowercase] => 1 [pos:protected] => 0 [char:protected] => [size:protected] => 0 [cursor:protected] => 1 [parent:protected] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => [_] => Array ( [0] => -1 [1] => 1 ) [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [token_blank:protected] => [token_equal:protected] => =/> [token_slash:protected] => /> [token_attr:protected] => > [self_closing_tags:protected] => Array ( [img] => 1 [br] => 1 [input] => 1 [meta] => 1 [link] => 1 [hr] => 1 [base] => 1 [embed] => 1 [spacer] => 1 ) [block_tags:protected] => Array ( [root] => 1 [body] => 1 [form] => 1 [div] => 1 [span] => 1 [table] => 1 ) [optional_closing_tags:protected] => Array ( [tr] => Array ( [tr] => 1 [td] => 1 [th] => 1 ) [th] => Array ( [th] => 1 ) [td] => Array ( [td] => 1 ) [li] => Array ( [li] => 1 ) [dt] => Array ( [dt] => 1 [dd] => 1 ) [dd] => Array ( [dd] => 1 [dt] => 1 ) [dl] => Array ( [dd] => 1 [dt] => 1 ) [p] => Array ( [p] => 1 ) [nobr] => Array ( [nobr] => 1 ) ) [doc:protected] => [noise:protected] => Array ( ) )

The code you posted is working fine for me.
Think you can try these, put below lines on top of script :
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
Also check print_r of $html
print_r($html);
Hope this help.

Related

Get a specific value from a multidimensional array provided by AgileCRM

I'm trying to get the first deal ID from AgileCRM.
When using:
$test = json_decode($deal, true);
print_r($test);
I get the following result:
Array (
[0] => Array (
[colorName] => WHITE
[id] => 5686812383117312
[apply_discount] =>
[discount_value] => 0
[discount_amt] => 0
[discount_type] => Value
[name] => New Home Loan
[contact_ids] => Array (
[0] => 5645056174194688
)
[custom_data] => Array (
)
[products] => Array (
)
[description] => New Lead
[expected_value] => 0
[milestone] => New Loan
[probability] => 10
[close_date] => 1521192269
[created_time] => 1510824270
[milestone_changed_time] => 0
[entity_type] => deal
[notes] => Array (
)
[note_ids] => Array (
)
[note_created_time] => 0
[pipeline_id] => 5719238044024832
[archived] =>
[lost_reason_id] => 0
[deal_source_id] => 0
[total_deal_value] => 0
[updated_time] => 1510824270
[isCurrencyUpdateRequired] => 1
[currency_conversion_value] => 0
[tags] => Array (
)
[tagsWithTime] => Array (
)
[contacts] => Array (
[0] => Array (
[id] => 5645056174194688
[type] => PERSON
[properties] => Array (
[0] => Array (
[type] => SYSTEM
[name] => first_name
[value] => piet
)
[1] => Array (
[type] => SYSTEM
[name] => last_name
[value] => pompies
)
[2] => Array (
[type] => SYSTEM
[name] => name
[value] =>
)
)
)
)
[owner] => Array (
[id] => 5178546118721536
[domain] => domainname
[email] => myemail#email.com
[phone] =>
[name] => Piet Pompies
[pic] => https://d1gwclp1pmzk26.cloudfront.net/img/gravatar/48.png
[schedule_id] => Piet Pompies
[calendar_url] => https://homeside.agilecrm.com/calendar/Piet_Pompies
[calendarURL] => https://homeside.agilecrm.com/calendar/Piet_Pompies
)
)
)
I want to echo "5686812383117312" from "[id] => 5686812383117312" (4th line in the array above)
I've tried "foreach" statements but my expertise on it is limited and can't seem to get it right.
Any help will be appreciated.
In order to access the ID field you should:
get the array's first key
Access the required field
Array:
Array ( //$test
[0] => Array ( //first key [0]
[colorName] => WHITE
[id] => 5686812383117312 //the required field ['id']
[apply_discount] =>
PHP:
$test = json_decode($deal, true);
print_r($test);
echo $test[0]['id']; //Output: 5686812383117312

PHP Simple HTML DOM Parser can't find elements after using str_get_html();

I am using PHP Simple HTML DOM Parser to gather some text from another website. When I use file_get_html($url) then I get the error, "Message: file_get_contents(http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html): failed to open stream: HTTP request failed! HTTP/1.1 416 Requested Range Not Satisfiable"
So I can use str_get_html($url) and I get a return but I can't find any elements afterward. Here is my code:
$data['yb_data_reviews'] = '';
$yb_reviews[] = '';
$yb_url = 'http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html';
if($yb_url){
$yb_dom = str_get_html($yb_url);
foreach($yb_dom->find('div.detailItem') as $yb_review) {
$item['author'] = $yb_review;
$yb_reviews[] = $item;
}
$data['yb_data_reviews'] = $yb_reviews;
}
print_r($data['yb_data_reviews']);
My return is blank. If I just return $yb_dom then I get:
simple_html_dom Object ( [root] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( [0] => simple_html_dom_node Object ( [nodetype] => 3 [tag] => text [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => simple_html_dom_node Object *RECURSION* [_] => Array ( [4] => http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) ) [parent] => [_] => Array ( [0] => -1 [1] => 2 ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [nodes] => Array ( [0] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( [0] => simple_html_dom_node Object ( [nodetype] => 3 [tag] => text [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => simple_html_dom_node Object *RECURSION* [_] => Array ( [4] => http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) ) [parent] => [_] => Array ( [0] => -1 [1] => 2 ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [1] => simple_html_dom_node Object ( [nodetype] => 3 [tag] => text [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( [0] => simple_html_dom_node Object *RECURSION* ) [parent] => [_] => Array ( [0] => -1 [1] => 2 ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [_] => Array ( [4] => http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) ) [callback] => [lowercase] => 1 [original_size] => 71 [size] => 71 [pos:protected] => 71 [doc:protected] => http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html [char:protected] => [cursor:protected] => 2 [parent:protected] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( [0] => simple_html_dom_node Object ( [nodetype] => 3 [tag] => text [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => simple_html_dom_node Object *RECURSION* [_] => Array ( [4] => http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) ) [parent] => [_] => Array ( [0] => -1 [1] => 2 ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [noise:protected] => Array ( ) [token_blank:protected] => [token_equal:protected] => =/> [token_slash:protected] => /> [token_attr:protected] => > [_charset] => UTF-8 [_target_charset] => UTF-8 [default_br_text:protected] => [default_span_text] => [self_closing_tags:protected] => Array ( [img] => 1 [br] => 1 [input] => 1 [meta] => 1 [link] => 1 [hr] => 1 [base] => 1 [embed] => 1 [spacer] => 1 ) [block_tags:protected] => Array ( [root] => 1 [body] => 1 [form] => 1 [div] => 1 [span] => 1 [table] => 1 ) [optional_closing_tags:protected] => Array ( [tr] => Array ( [tr] => 1 [td] => 1 [th] => 1 ) [th] => Array ( [th] => 1 ) [td] => Array ( [td] => 1 ) [li] => Array ( [li] => 1 ) [dt] => Array ( [dt] => 1 [dd] => 1 ) [dd] => Array ( [dd] => 1 [dt] => 1 ) [dl] => Array ( [dd] => 1 [dt] => 1 ) [p] => Array ( [p] => 1 ) [nobr] => Array ( [nobr] => 1 ) [b] => Array ( [b] => 1 ) [option] => Array ( [option] => 1 ) ) )
What am I doing wrong that I can't return any elements using str_get_html()? Any help is appreciated. Thanks.

getting title field from the process array in the webform

I am using the webform module for Drupal.
I needed to customize one of the webform nodes.
So I tried printing [title] => title of this webform goes here!!! using <?php print drupal_render($form['process']['title']); ?>
But it returns nothing, what am I doing wrong here?
Thanks!
FYI, the form can be rendered with; drupal_render($form['submitted']);
Array ( [#attached] => Array ( [css] => Array ( [0] => sites/all/modules/webform/css/webform.css ) [js] => Array ( [0] => sites/all/modules/webform/js/webform.js ) ) [#process] => Array ( [0] => webform_client_form_includes ) [#node] => stdClass Object ( [vid] => 22 [uid] => 2 [title] => title of this webform goes here!!! [log] => [status] => 1 [comment] => 1 [promote] => 1 [sticky] => 0 [nid] => 22 [type] => webform [language] => en [created] => 1373585579 [changed] => 1374117294 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1374117294 [revision_uid] => 8 [body] => Array ( ) [rdf_mapping] => Array ( [rdftype] => ...
You should try this,
echo $form[#node]->title;
var_dump($form[#node]);// to test it your node data
As from your array
Array (
[#attached] => Array ([css] => Array ( [0] => sites/all/modules/webform/css/webform.css ) [js] => Array ( [0] => sites/all/modules/webform/js/webform.js ))
[#process] => Array ( [0] => webform_client_form_includes )
[#node] => stdClass Object ( [vid] => 22 [uid] => 2 [title] => title of this webform goes here!!! [log] => [status] => 1 [comment] => 1 [promote] => 1 [sticky] => 0 [nid] => 22 [type] => webform [language] => en [created] => 1373585579 [changed] => 1374117294 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1374117294 [revision_uid] => 8 [body] => Array ( ) [rdf_mapping] => Array ( [rdftype] => ...

Pull out a PHP variable in a complex nested array (from a Drupal View) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code
I've got a complex array variable called $data. I need to pull out one very buried value: [url] in [field_website]. Here's the raw print_r() output of the $data variable:
stdClass Object ( [node_title] => CMI2 [nid] => 3 [field_data_field_website_node_entity_type] => node [field_data_field_blog_node_entity_type] => node [field_data_field_rss_node_entity_type] => node [field_data_field_twitter_node_entity_type] => node [field_data_field_yammer_node_entity_type] => node [field_data_field_facebook_node_entity_type] => node [field_data_field_flickr_node_entity_type] => node [field_data_field_youtube_node_entity_type] => node [_field_data] => Array ( [nid] => Array ( [entity_type] => node [entity] => stdClass Object ( [vid] => 3 [uid] => 1 [title] => CMI2 [log] => [status] => 1 [comment] => 1 [promote] => 0 [sticky] => 0 [nid] => 3 [type] => social_source [language] => und [created] => 1356040541 [changed] => 1356040541 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1356040541 [revision_uid] => 1 [field_website] => Array ( [und] => Array ( [0] => Array ( [url] => http://cmi2.yale.edu [title] => [attributes] => Array ( ) ) ) ) [field_blog] => Array ( ) [field_rss] => Array ( ) [field_twitter] => Array ( [und] => Array ( [0] => Array ( [url] => http://twitter.com/yalecmi2 [title] => [attributes] => Array ( ) ) ) ) [field_facebook] => Array ( ) [field_youtube] => Array ( ) [field_flickr] => Array ( ) [field_yammer] => Array ( ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item [1] => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date [1] => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1356040541 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => admin [picture] => 0 [data] => b:0; ) ) ) [field_field_website] => Array ( [0] => Array ( [rendered] => Array ( [#markup] => http://cmi2.yale.edu [#access] => 1 ) [raw] => Array ( [url] => http://cmi2.yale.edu [title] => http://cmi2.yale.edu [attributes] => Array ( ) [display_url] => http://cmi2.yale.edu ) ) ) [field_field_blog] => Array ( ) [field_field_rss] => Array ( ) [field_field_twitter] => Array ( [0] => Array ( [rendered] => Array ( [#markup] => http://twitter.com/yalecmi2 [#access] => 1 ) [raw] => Array ( [url] => http://twitter.com/yalecmi2 [title] => http://twitter.com/yalecmi2 [attributes] => Array ( ) [display_url] => http://twitter.com/yalecmi2 ) ) ) [field_field_yammer] => Array ( ) [field_field_facebook] => Array ( ) [field_field_flickr] => Array ( ) [field_field_youtube] => Array ( ) )
(sorry about that ugliness!)
How the heck do I pull out that [url] variable?! Ideally, I just want to assign that one value to another variable or just print it out.
If it's helpful, this is from a Drupal 7 view with the Views PHP module.
Thanks!
I recommend reading up about PHP arrays and objects since what you are trying to do is so trivial.
http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/sdo.sample.getset.php

problem assigning array to variable

I'm sure this is a simple one. I have an array in a simplexml object. When I try to assign the array to a variable, it only assigns the first index of the array. How can I get it to assign the whole array. This is my code.
$xml = simplexml_load_string(FlickrUtils::getMyPhotos("flickr.photos.search", $_SESSION['token']));
$photosArray = $xml->photos;
//$photosArray = $xml->photos->photo;
//echo gettype($photosArray);
print_r($photosArray);
This is the result of the print_r($photosArray);
SimpleXMLElement Object
(
[#attributes] => Array
(
[page] => 1
[pages] => 1
[perpage] => 100
[total] => 4
)
[photo] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 5335626037
[owner] => 57991585#N02
[secret] => bd66f06b49
[server] => 5210
[farm] => 6
[title] => 1
[ispublic] => 1
[isfriend] => 0
[isfamily] => 0
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 5336238676
[owner] => 57991585#N02
[secret] => 898dffa011
[server] => 5286
[farm] => 6
[title] => 2
[ispublic] => 1
[isfriend] => 0
[isfamily] => 0
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 5335625381
[owner] => 57991585#N02
[secret] => 60a0c84597
[server] => 5126
[farm] => 6
[title] => 4
[ispublic] => 1
[isfriend] => 0
[isfamily] => 0
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 5335625195
[owner] => 57991585#N02
[secret] => 49348c1e8b
[server] => 5126
[farm] => 6
[title] => 3
[ispublic] => 1
[isfriend] => 0
[isfamily] => 0
)
)
)
)
Thanks for youe help!
I fail to see an array in your example. However, $xml is traversable, so you probably mean that. $xml->photos selects only the first photo element though. You are probably looking for
$photosArray = $xml->xpath('//photo');
which indeed returns an array.
In order to return all photo, can make used on children()
You can cast the list of simplexml objects into array, like
$photosArray = (array)$xml->children();
/* or retain the simplexml object */
$photosArray = $xml->children();

Categories