PHP – How to use curl to get content of a website?

As I developed my own small applications with Silverlight, I have difficulties with Cross-Site-Domain access. Because of preventing potential networking threats to be mitigated include:

  • Denial of Service (DoS) attacks – A large number of remote hosts are used to attack a target site sothat the target is unable to service valid requests.
  • DNS Rebinding attacks – Use DNS to force a remote host to rebind a trusted host name (site oforigin) to a victim’s IP address, thus allowing access to a host other than site of origin.
  • Reverse tunnel attack – Use a remote client’s outgoing connection as a back tunnel to the client’sprivate network

Therefore Silverlight requires a Security Policy System based on a xml file which will be put at the cross domain site. If he found this file on cross domain site, he will check the security policy. If he is allowed to access, then he’ll create a connection to it and start to transfer the data. If not, he will give an error with access limitation back.  This policy is good for avoiding all discussed threats above but it limits the functionalities of tools too.

For example I would like to call a url from a website and get its content back. I can not do it easily with Silverlight. I need to ask the author of website to put a xml file which allows my program to access. It is so complicated and I am sure that no author will allow you without gaining any benefit.

So I must find a way to get the content of that website through my website and Silverlight will connect to my site, get the content because I have full permission to my website. I can allow which tool to access my site. My site will work as a webservice which can be implemented in ASP or PHP.

Now it is time to decide which language should be used. I have used a month to ask my friends to share me a windows host to test some of my webservices. However none of my friend can help me. They all use Linux host. Thus I have no choice any more, I must use PHP. And today I write a very small code to get direct link of a song from http://chacha.vn as my first lesson with PHP.

Chacha.vn is music website, you can find there a lot of songs (vietnames, international) in MP3 quality. However they turned off the download function so if you want to download songs you need some add-ons or tool to get direct link of song. I think it is good to write my first tool in PHP with getting direct link. It must be not so difficult and suitable for beginners. Here is my code in PHP and here is tool to get direct link of chacha.vn.


<html>
<head>
<title> Tool to get download link from http://chacha.vn</title>
</head>
<body>
<form method="post" action="chacha.php">
<p>Tool to get download link of a song at site http://chacha.vn</p>
Enter id of song
<input type="text" name="txtSongId" value="<?php echo $_POST['txtSongId']; ?>">
<input type="submit" name="btnSubmit" value="Get link">
<input type="hidden" name="bSubmitted" value=1>
</form>

<?php
if (isset($_POST['bSubmitted']))
{
$strSongId = (string) $_POST['txtSongId'];
$strLinkMp3 = "http://chacha.vn/song/getUrl?id=".$strSongId."&songTable=1";
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$strLinkMp3);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$strContent = curl_exec($curl_handle);
curl_close($curl_handle);

if (empty($strContent))
{
print "Sorry, the id you entered may be wrong or the site chacha.vn is now not accessible";
}
else
{
print $strContent;
}
}
?>

</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *