Reputation: 322
I want to get the source code of Youtube URL (https) which is similar to what we see on - "View page source" option in browser.
Following is my php code - (index.php)
<?php
function gethtml($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$ip=rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"));
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/".rand(3,5).".".rand(0,3)." (Windows NT ".rand(3,5).".".rand(0,2)."; rv:2.0.1) Gecko/20100101 Firefox/".rand(3,5).".0.1");
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$url = $_REQUEST["url"];
$html = gethtml($url);
echo htmlspecialchars($html);
}
?>
<html>
<head></head>
<body>
<form name="test" method="POST" action="./index.php"/>
URL : <input type="text" name="url"/>
<br>
<input type="submit" value="See HTML" name="submit"/>
<br>
</form>
</body>
</html>
It works for other URL'S but not for any youtube URL. Why ?
Upvotes: 1
Views: 2222
Reputation: 141
If you're not adamant on cURL you can just use:
file_get_contents();
That will return a url resource as a string, so:
echo file_get_contents('https://www.youtube.com/watch?v=fyLGa0E3OXk');
That would print the source of the URL given.
Edit because of comment about header:
You can pass file_get_contents
a context resource created with stream_context_create()
.
Upvotes: 1
Reputation: 78
You can try this:
<?php
function getSSLPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
var_dump(getSSLPage($_POST["url"]));
?>
Upvotes: 2