-2

Someone was nice to write me a script that allows to retrieve data from cross domain site. The PHP looks like this:

<?php
if(isset($_GET['site'])){
  $f = fopen($_GET['site'], 'r');
  $html = '';
  while(strpos($html, 'position1_article_1') === FALSE)
    $html .= fread($f, 24000);
  fclose($f);
  echo $html;
}
?>

Which is saved as proxy.php The Jquery part looks like this:

$(function(){

   var site = 'http://www.nu.nl';

   $.get('proxy.php', { site:site }, function(data){

      var href = $(data).find('.hdtitle').first().children(':first-child').prop('href');
      var url = href.split('/');
      href = href.replace(url[2], 'nu.nl');

      // Put the 'href' inside your div as a link
      $('#myDiv').html('<a href="' + href + '" target="_blank">' + href + '</a>');

   }, 'html');

});

The problem is that I cant figure out how to get all the 'a' in the body or certain part like h2, h3 etc. Can someone please break it down to me? For instance; what do I have to change in order to get all the href in the page?

techfoobar
  • 65,616
  • 14
  • 114
  • 135
Youss
  • 4,196
  • 12
  • 55
  • 109
  • I'm not too sure in what you're trying to do, could you re-word it slightly? – narruc Nov 04 '12 at 11:33
  • http://stackoverflow.com/questions/3627489/php-parse-html-code – ethrbunny Nov 04 '12 at 11:34
  • @ShaneCurran I want to do several things with the script, but I cant figure out how it works(I dont know PHP) So if someone could explain to me how to get all href of the body, then this could bring me a step closer to understandin how it works. – Youss Nov 04 '12 at 11:37
  • I dont know how your html looks like but you need to change this line, var href = $(data).find('a').prop('href'); should give you all the a tags. – defau1t Nov 04 '12 at 11:38
  • @defau1t I think I wouldn't because of this line: while(strpos($html, 'position1_article_1') === FALSE) – Youss Nov 04 '12 at 11:39
  • That php code is doing nothing more than loading the html you point at, and stopping when it finds the string "position1_article_1" in the response. If you want to parse the html see @ethrbunny's comment, or one of the 10s/100s of duplicates where parsing html with php is discussed. – AD7six Nov 04 '12 at 23:46
  • 2
    @Youss you don't need to make duplicate questions, check this: http://stackoverflow.com/a/11887788/1178686 it is solved now. – Oscar Jara Nov 05 '12 at 00:14
  • @Oscar Jara Thanks for taking another look at it:) – Youss Nov 05 '12 at 14:23

1 Answers1

-1

Try this : header('Access-Control-Allow-Origin: *');

And use it like that :

<?php
header('Access-Control-Allow-Origin: *');
if(isset($_GET['site'])){
  $f = fopen($_GET['site'], 'r');
  $html = '';
  while(strpos($html, 'position1_article_1') === FALSE)
    $html .= fread($f, 24000);
  fclose($f);
  echo $html;
}
?>

Enjoy !

Random78952
  • 1,520
  • 4
  • 26
  • 36
  • How is adding `Access-Control-Allow-Origin` going to do anything at all when the php request is to the same domain as the page loading the js file? – AD7six Nov 04 '12 at 23:38