0

I have a piece of code (CURL) from which I can extract data like this:

  <?php
    $url = "http://www.nu.nl/";

    $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 HTML file with an input. I would like to replace the physical URL with a dynamic one, so setting it as a variable. The variable should be the value of the input which is in the parent document. So the php file is echoed in an iframe. Is this possible? I have Googled and search Stack but I only found this answer: LINK Which I could not alter to want I want. (I'm new at PHP)

Community
  • 1
  • 1
Youss
  • 4,196
  • 12
  • 55
  • 109
  • How many PHP/HTML files are you dealing with? And how many requests to user's browser in this process? Can you provide a little more info about what this script's purpose is? – JRL Apr 15 '13 at 19:22
  • I am not sure I understand your question, are you trying to make some kind of "proxy script" to get the HTML code of a web page? For example, something that you call as ` – Ale Apr 15 '13 at 19:23
  • Please underline (and add the code that has it if not already in there) the physical URL in your question you want to replace. Right now in your question it is not clear what you're asking about. – M8R-1jmw5r Apr 15 '13 at 19:24
  • @JRL Hi, I'm not sure what you mean..This is just my own little project, using my own browser. I only have one php file with only the code above inside it and HTML file with an input. At this point I'm only interested in "changing the URL with input" – Youss Apr 15 '13 at 19:25
  • @M8R-1jmw5r Hi, the physical URL is "http://www.nu.nl/" as in the question. However I need it to be a variable. I know in Jquery I would do something like this: `var $variable = $(parent.document.body).find('input').val();` – Youss Apr 15 '13 at 19:28
  • @user1532999: Okay, good to know, I suggest to read it how this works in PHP from the manual: http://php.net/manual/en/language.variables.external.php – M8R-1jmw5r Apr 15 '13 at 19:30
  • @Ale Hi, Yes Im trying to make some sort of proxy script, actually I already have the script which is the code above:-) Like I mentioned, Im new at PHP. I know this should be very easy to do but I can't figure it out. – Youss Apr 15 '13 at 19:31
  • @M8R-1jmw5r Thanks for the link, cant give you +1, need reputation.. – Youss Apr 15 '13 at 19:48

1 Answers1

1

You would have to change your echo line in your PHP file to this:

echo base64_encode($curl_scraped_page);

And your $url assignment to this:

$url = $_POST['url'];

And use this jQuery (you can use plain JavaScript if you wish, jQuery is just nicer to work with):

$('#getPage')click(function() { //When the button "getButton" is clicked
    //Make a POST request to your PHP file with the value of the textbox as the URL
    $.post('myCURLPage.php', {url: $('#myTextInput').val()}, function(encodedData) {
        //Set the iFrames "src" attribute to the base64 encoded HTML page returned from you r PHP script
        $('#myiFrame').attr('src', 'data:text/html;base64,'+encodedData);
    });
});

(This presumes you have a button on your page with the ID set to getPage and the iFrame with an ID of myiFrame and an input box with an ID of myTextInput)

Disclaimer: I've not tested this code, but theoretically it should work.

OdinX
  • 4,135
  • 1
  • 24
  • 33
  • Testing it, one moment please – Youss Apr 15 '13 at 19:36
  • Hi, I have tested it on my server and it doesn't work. Nothing happened on the click of button, console is not giving errors. Actually the call to myCURLPage.php is never made..I have put an example at Jsfiddle http://jsfiddle.net/BsKct/ I know it doesn't work there but I just want to show you what I did. I put the proxy in the CSS department (Cant give you +1 need Reputation..) – Youss Apr 15 '13 at 19:45
  • Isn't base64_encode for protecting code? I don't really need that since Im only giving URL – Youss Apr 15 '13 at 19:47
  • Nope, base64 is just a way of encoding data. Have a read of this: http://stackoverflow.com/questions/201479/what-is-the-use-of-base-64-encoding – OdinX Apr 15 '13 at 19:49
  • I don't thing that it can work on jsfiddle, putting PHP code in the CSS section wont run the code... – Ale Apr 15 '13 at 19:50
  • http://jsfiddle.net/BsKct/4/ Copy that HTML into your file. The jQuery code needed to be inside the `$(document).ready` function because it was trying to bind an event to an element that didn't exist yet. – OdinX Apr 15 '13 at 19:57
  • I removed the click event and now the call is made. But now I get this in Iframe: `src="data:text/html;base64, "` The Iframe is empty, no data – Youss Apr 15 '13 at 19:59
  • Hard code a URL in your PHP file and check the base64 encoded output. it should look like a random bunch of alpha/numeric characters – OdinX Apr 15 '13 at 20:02
  • I copied your code, same result. The Iframe gets a src like this `src="data:text/html;base64, "` The Iframe however is empty. I don't know what the src 'code' means. It seems its just calls for what you have put in Jquery `$('#myiFrame').attr('src', 'data:text/html;base64` – Youss Apr 15 '13 at 20:04
  • I have hardcode the URL and now Im getting a very long line of letters (not numbers) which don't make sense – Youss Apr 15 '13 at 20:05
  • Ok, you *should* just need to change the `$_GET` to `$_POST` (my bad) – OdinX Apr 15 '13 at 20:15
  • Thank you VERY MUCH:-) Its a work of art. Im sorry I cant give you +1 for effort due to no "reputation" – Youss Apr 15 '13 at 20:24