0

I have a screen scrape code in PHP like this:

<?
$url = 'https://www.google.nl/search?q=cars';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
?>

I also have a Jquery(ajax) script which can fetch the proxy like this:

$.get('ThePhpFile.php', function(data){

     $(data).appendTo('div')

}

Everything is working fine, but now I would like to set the URL to be scraped as a variable which is in fact the value of some input in parent document (I'm working with iframes). I know how to do this in Jquery:

var TheUrl = $("input", parent.document.body).val();

So my question is how do I set the variable to work with the PHP code. Is it necessary to put it in the PHP code? How do I do that?

2
  • You need to ajax the variable to the php Commented Mar 9, 2013 at 8:57
  • @mplungjan I already tried several things but with no luck(not good at PHP) If I Google I only encounter codes which go in the opposite direction PHP variable ----> Jquery Commented Mar 9, 2013 at 9:00

1 Answer 1

1

You need to ajax the variable to the php:

This will send ?url=..... to the php

Full example DEMO

<html>
<head>
<title>Search Proxy</title>
<script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>
<script>
$(function() {
  $("#search").on("submit",function(e){
    e.preventDefault();
    $.get('searchproxy.php', 
     {url:$("#url").val()}, 
       function(data){
         $(data).html('#result');
     });
  });
});
</script>
</head>
<body>
<div>
<form id="search">
<input id="url" type="text" value="" />
</form>
<div id="result"></div>
</body>
</html>

<?php
$url = $url = $_GET["url"]; // you need to add input cleaning
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
?>
Sign up to request clarification or add additional context in comments.

8 Comments

Im getting 'missing name in function' error when I put it in JsFiddle jsfiddle.net/4ZBEf
Also your fiddle has no html and is not running jQuery
But how do I relate this to the PHP code.. If I remove the URL part($url = 'google.nl/search?q=cars';) it doesn't work..
I mean something like this $url = TheUrl();
It doesn't work for me..Could the problem be parent.document.body).val() I would really appreciate help with this lat bit..
|

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.