Many servers that are leased as virtual hosts tend to disable file_get_contents, Fsockopen, and other IO-operated functions because they are afraid of being DDOS.
But many webmasters have to use this function to crawl URL page content, for example, I need to crawl the major web site ued Blog RSS content, output to my home page.
Under normal circumstances, we can not change the server's inc.php, only write a set of IO to replace the above PHP function.
$url = file_get_contents (' http://www.chongqingwangzhai.com/');
We can use the following code instead
Workaround for disabling file_get_contents
$ch = Curl_init ();
$timeout = 10; Set to zero for no timeout
curl_setopt ($ch, Curlopt_url, ' http://www.chongqingwangzhai.com/');
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
$url = curl_exec ($ch);
Curl is a tool that uses URL syntax to transfer files and data, and supports many protocols, such as HTTP, FTP, Telnet, etc., which will not be disabled by the server, so we can use the same URL to simulate file_get_contents.
and generally we want to crawl page data, such as Sina Weibo, Baidu Forum and so on page, all need to login to enter the list page, so this time need to use Curl analog login, and then open the URL, the principle is to use Curl set HTTP access to the head information, analog login header information, Let the other server think you are in the login state. The concrete realization method does not say, the network many.
Hope this solution can help some webmaster.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.