-1

I want to read other sites on my web with file_get_contents(). My value can be with javascript merging.

My site page is : http://example.com/page.html#2010/09/awesome.html

JAVASCRIPT

var hash = window.location.hash.substr(1);
// results: 2010/09/awesome.html

PHP

$hash = "<script>document.write(hash);</script>";
$resultLink = "http://www.external-site.com/" .$hash;
$content = file_get_contents($resultLink);
$first_step = explode("<div class='post-entry'>" , $content );
$second_step = explode("</div>" , $first_step[1] );

echo $second_step[0];

Here $resultLink is : http://www.external-site.com/2010/09/awesome.html But file_get_contents($resultLink) can not open the page.

How can I fix this code?

dBoys
  • 31
  • 6
  • See here: https://stackoverflow.com/questions/7794604/file-get-contents-not-working – trapper Apr 28 '17 at 05:08
  • 5
    Possible duplicate of [file\_get\_contents not working?](http://stackoverflow.com/questions/7794604/file-get-contents-not-working) – trapper Apr 28 '17 at 05:09
  • What errors are you receiving? – Enstage Apr 28 '17 at 05:10
  • @Enstage Content on external-site can not be opened with `file_get_contents` – dBoys Apr 28 '17 at 05:13
  • I suspect you're trying to execute JS in PHP / PHP in JS, which is not how it works. JS is executed client side (in the user's browser), PHP is server side. – Enstage Apr 28 '17 at 05:14
  • @EnstageYes, then how the correct code for that – dBoys Apr 28 '17 at 05:19
  • Possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – John V. Apr 28 '17 at 05:31

1 Answers1

0

The problem here is JS is executed client side, which is AFTER the PHP is executed and produces content to send to the browser.

For example, by doing what you're doing $resultLink contains http://www.external-site.com/<script>document.write(hash);</script>

Enstage
  • 2,106
  • 13
  • 20