DelightedD0D DelightedD0D - 6 months ago 83
Javascript Question

Get jsFiddle result iframe source code with php

I have a jsFiddle that lives a happy life at https://fiddle.jshell.net/DelightedDoD/yu6nekv4/show/light/

If I navigate to

view-source:https://fiddle.jshell.net/DelightedDoD/yu6nekv4/show/light/
I can view the full rendered source code for my fiddle.

I need to get this full rendered source code in a php script.

Here is what I've tried:

file_get_contents():



$src = file_get_contents('https://fiddle.jshell.net/DelightedDoD/yu6nekv4/show/light/');
echo '<textarea>'.$src.'</textarea>';


curl:



$ch = curl_init('https://fiddle.jshell.net/DelightedDoD/yu6nekv4/show/light/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
echo '<textarea>'.$content.'</textarea>';


In both cases what I end up with is the source code of the page that renders my fiddle's source code into an iFrame not the source code of my actual fiddle

To see the result, go to http://dodsoftware.com/shared-resources/php/jsfiddle-mobile-bs-frame.php

Is there some way, I can replicate the result I get with
view-source:
using PHP or at least get the value then send it to my php script?


Answer

You just need to set the referer in the curl options to the same page you are loading:

$url = 'https://fiddle.jshell.net/DelightedDoD/yu6nekv4/show/light/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
$content = curl_exec($ch);
curl_close($ch);
echo '<textarea>'.$content.'</textarea>';
Comments