我有一个jsFiddle,在https://fiddle.jshell.net/DelightedDoD/yu6nekv4/show/light/
过着幸福的生活如果我导航到view-source:https://fiddle.jshell.net/DelightedDoD/yu6nekv4/show/light/
,我可以查看完整的渲染源代码。
我需要在php脚本中获取这个完整的渲染源代码。
这是我尝试过的:
$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>';
在这两种情况下,我最终得到的是页面的源代码,它将我的小提琴的源代码呈现为iFrame 而不是我的源代码实际小提琴
要查看结果,请转到http://dodsoftware.com/shared-resources/php/jsfiddle-mobile-bs-frame.php
view-source:
获得的结果,或者至少获取值然后将其发送到我的PHP脚本?答案 0 :(得分:1)
您只需将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>';
答案 1 :(得分:1)
尝试:
$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>';