1

I have used curl to fetch a webpage. There are links on this page. On the curl fetched page when I click a link I want JQuery to execute a curl script to fetch that link. I have the following code..

This is my index page which fetches example.com via curl and echos it out.

<html>
<head>
<title>Public</title>
<script type="text/javascript" src="click.js"></script>;
</head>

<?php
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'www.example.com');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($ch);
curl_close($ch);
echo $curl_response;
?>

click.js I'm targeting the tag so when any link is clicked this will execute. Will this work?

$(".a").click(function(){
$.getScript("curllink.php"); 
});

This is the script I want the js to run when a link with the tag curllink.php

<?php
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'WHAT DO I PUT HERE?');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($ch);
curl_close($ch);
echo $curl_response;
?>

Please could you check over this and fix any errors I have made. Thanks

5
  • I have so many questions... why are you trying to run before you can walk? firstly where is the <a> tag, 2. whats $.getScript()? 3. are the content within your website? or a thirdparty website? Commented May 17, 2011 at 11:02
  • Because I'm trying to do something and it involves me having to do this. The first code block is my site. It fetches example.com and echo's it so my site looks exactly the same as example.com. I then click a link on my site. It now redirects to example.com (original site) which I don't want. I want it to fetch the example.com page the link directed to and display it on my site. The <a> tag is every link is an <a> tag. The rest I don't know I'm trying to find an answer by piecing together stuff that may wok. That's why I'm asking for help.. Commented May 17, 2011 at 14:21
  • Are you trying to mislead people ? because you aint getting any help from me for that lol, Commented May 17, 2011 at 15:19
  • No not at all. Its a app with a web frontend and I need to redisplay it to create a online version. Promise there is no misleading at all. Commented May 17, 2011 at 15:21
  • Check johns answer for the jQuery Commented May 17, 2011 at 15:25

1 Answer 1

1

Although you'll still need to do some parsing of the incoming file, I think you want to intercept the script call and prevent the default behavior.

Your jquery would look more like this:

$('a').click(function(){
  location.href='curllink.php?url='+escape($(this).attr('href'));
  return false;
});

PHP looks like this (removing CURL for simplification:

<?php 
  echo file_get_contents($_REQUEST['url']);

I haven't tested it, but that should work... Shouldn't need to escape the incoming URL parameter.

Updated JQuery You will need to put this after you get the page contents.

$('a').live('click',function (e){
   var href = $(this).attr('href');
   href= href.replace('example.com','your_website.com');
   e.preventDefault();
   window.location.href = href;
   return ;
});
Sign up to request clarification or add additional context in comments.

6 Comments

What about the other files, curllink.php what do I do to fix that?
The 2nd block (PHP) is the entirety of my suggested replacement for curllink.php.
Actually, this is problematic... This doesn't take relative URLs into account. You'd need to do some parsing of the URLs for that... most likely in the JavaScript.
But surely if there is no curl in curllink.php then its just going to navigate to the original file..
@Michael curl its a php library or extention if you like, it gets a page (by url) then gets it's contents, and headers and anything else useful and manipulates it, most search engines use this method to capture website data.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.