I am writing a site on php. A small working parser of news articles is written from a certain news resource. However, the parser is working until it becomes necessary to use javascript.
As planned, the parser should work as follows:
- In the news_highlights.php file there is a code that parses the headlines of the articles and displays them on my main page.
- There is a script.js file with javascript code, which, upon clicking on a specific header, removes the link to the full text of the selected article in the source and places this link in a javascript session using the sessionStorage.setItem command.
- There is a file page-news-article.php, which contains the code, which, in fact, parses the entire article, using the link obtained in paragraph 2.
The contents of the files - below.
Question: why in php file number 3 I can get the value of the variable js (link to the source article) and display it via echo on the page, but the php script in the same file number 3 refuses to accept it? How can I get php to accept a variable (link to the source article) and execute the script?
Immediately, I note that Ajax doesn't suit me, I need the article to open on another page. Although with Ajax everything works like a clock from start to finish :)
In the browser I get the following:
http://some-site.com/information/prime-anonce-38.html
Warning: file_get_contents (var data = sessionStorage.getItem ('setUrl'); document.write (data);) [function.file-get-contents]: failed to open stream: Invalid argument in Z: \ home \ my-site \ www \ wp-content \ themes \ WP_CLEAN \ simple_html_dom.php on line 76
Fatal error: Call to a member function find () on a non-object in Z: \ home \ my-site \ www \ wp-content \ themes \ WP_CLEAN \ page-news-article.php on line 14
- news_highlights.php
<?php include 'simple_html_dom.php'; $html = file_get_html(some-site.com); $a = 1; $b = 1; $c = 1; foreach($html->find('div.post-feed article a, article picture.icon-wrapper img, div.post-feed article h3, div.post-feed article p') as $highlights) { if ($highlights->tag == 'a'): echo '<div id="news-highlight-'.$a++.'" class="news-highlights"><a id="href-to-source-article-'.$b++.'" class="href-to-source-article" href="http://some-site.com'.$highlights->href.'"><a id="article-'.$c++.'" class="article" href="http://my-site/article/"><article>'; elseif ($highlights->tag == 'img'): echo '<img src="http://some-site.com'.$highlights->src.'" alt="'.$highlights->alt.'">'; elseif ($highlights->tag == 'h3'): echo $highlights; elseif ($highlights->tag == 'p'): echo $highlights . '</article></a></a></div>'."\n"; endif; } } ?>
- script.js
$(document).on('click','.news-highlights', function(){ var id = $(this).attr('id'); var id1 = $('.href-to-source-article', this).attr('id'); var valUrl = $('#' + id1).attr('href'); sessionStorage.setItem('setUrl', valUrl); var data = sessionStorage.getItem('setUrl'); alert(data); document.location.href = 'http://agama/wp-content/themes/WP_CLEAN/page-news-article.php'; return false; });
- page-news-article.php
<?php include 'simple_html_dom.php'; $q = "<script> var data = sessionStorage.getItem('setUrl'); document.write(data); </script>"; echo $q; $html = file_get_html($q); $a = 1; $b = 1; $c = 1; foreach($html->find('div.article-area div.column-non-right article h1, div.article-area div.column-non-right article div.article-text p, div.article-area div.column-non-right article div.article-text img') as $highlights) { if ($highlights->tag == 'h1'): echo $highlights; elseif ($highlights->tag == 'p'): echo $highlights; elseif ($highlights->tag == 'img'): echo '<img src="'.$highlights->src.'" alt="'.$highlights->alt.'">'; endif; } ?>