Michael
Michael

Reputation: 479

JQuery on link click to execute curl

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

Upvotes: 1

Views: 2493

Answers (1)

John Green
John Green

Reputation: 13435

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

Upvotes: 1

Related Questions