使用 php 获取 jsFiddle 结果 iframe 源代码

Get jsFiddle result iframe source code with php

我有一个jsFiddle,在https://fiddle.jshell.net/DelightedDoD/yu6nekv4/show/light/

过着幸福的生活

如果我导航到 view-source:https://fiddle.jshell.net/DelightedDoD/yu6nekv4/show/light/,我可以查看 fiddle.

的完整渲染源代码

我需要在 php 脚本中获取完整呈现的源代码。

这是我试过的方法:

file_get_contents():

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

卷曲:

$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>';

在这两种情况下,我最终得到的是页面的 源代码,它将我的 fiddle 的源代码呈现为 iFrame 不是我实际fiddle

源代码

要查看结果,请转至 http://dodsoftware.com/shared-resources/php/jsfiddle-mobile-bs-frame.php

有没有什么办法,我可以使用 PHP 复制我通过 view-source: 获得的结果,或者至少获取值然后将其发送到我的 php 脚本?

您只需将 curl 选项中的 referer 设置为您正在加载的同一页面:

$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>';

尝试:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://fiddle.jshell.net/DelightedDoD/yu6nekv4/show/light/"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = array();
$headers[] = 'Connection: keep-alive';
$headers[] = 'Referer: http://fiddle.jshell.net/DelightedDoD/yu6nekv4/show/light/';
$headers[] = 'DNT: 1';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
$headers[] = 'Accept-Language: en-US,en;q=0.8';
$headers[] = 'Cache-Control: max-age=0';
// $headers[] = 'Accept-Encoding: gzip, deflate, sdch';
$headers[] = 'Host: fiddle.jshell.net';
$headers[] = 'Upgrade-Insecure-Requests: 1';

$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.10 Safari/537.36';


curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$server_output = curl_exec ($ch);

curl_close ($ch);

echo '<textarea>'.$server_output .'</textarea>';