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:

  1. In the news_highlights.php file there is a code that parses the headlines of the articles and displays them on my main page.
  2. 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.
  3. 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

  1. 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; } } ?> 

  1. 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; }); 

  1. 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; } ?> 

  • 2
    That moment when you don't know, edit the title of the question, or wait until someone more boring makes it, or some kind of karmofermer - Duck Learns to Take Cover
  • That moment when you look at the code and just fuck up))) - yarkov_aleksei
  • That moment when you write a comment just to continue the topic. - user207618 8:50 pm
  • That moment when you are bore)) Give better advice on my question, give :) I have been suffering for the second week, and this is not such a perversion in the code that has been in search of a solution during this time))) - Antonio
  • The moment when he came and renamed the question, breaking the raspberries into boredom, karmofermer and commentators - Kromster

1 answer 1

Do you even understand what the code does:

 $q = "<script> var data = sessionStorage.getItem('setUrl'); document.write(data); </script>"; echo $q; 

What does the $ q variable contain? If you think that the URL is in it, then I have bad news))) On the JS side, write in cookies, not in sessionStorage. And on the php side, read as expected from the $ _COOKIE array.

  • I am not a programmer by training, although I like it and everything that is written in these codes is written by me, even if it is clumsy))) but I understand what this code does. Another question is: why does it display the link through echo, but the php script does not? Cookies try, but what are the disadvantages / advantages of using cookies? The principle of how to get - through the link? - Antonio
  • Yes, by the way, in the js file there is a lot of garbage like alert, etc. - it was done for experiments and everything was cleaned in the end, when I found the answer to the question, so do not kick hard for the bad code)) - Antonio
  • And you instead of "echo $ q;" write "var_dump ($ q);" and see the difference. And still use cookies or pass the URL via the GET parameter. - yarkov_aleksei
  • How to be? Used cookies - accepts a variable, but not its value (not a link). script.js $ (document) .on ('click', '. news-highlights', function () {var id = $ ('. href-to-source-article', this) .attr ('id') ; var valUrl = $ ('#' + id) .attr ('href'); document.cookie = "setUrl = valUrl"; document.location.href = ' my-site / wp-content / themes / WP_CLEAN / page -news-article.php '; return false;}); In php, I accept it and try to output it to check: $ q = $ _COOKIE ['setUrl']; echo $ q; - Antonio