Web page capturing-web page capturing with curl in php

Source: Internet
Author: User
{Code ...}
Encapsulate a function that captures web pages with curl. it can be tested locally. after it is placed on the test server, if it is accessed and executed through a browser, the HTTP status code returned by the function returns 0 in most cases, error message 'error: name lookup timed out'. if it is an idol, 200 is returned successfully. if it is directly executed on the test server using the command line, 100% is successful. The code is as follows:
Static public function curlGet ($ url, $ data = array (), $ header = array (), $ timeout = 3, $ port = 80) {$ is_ssl = substr ($ url, 0, 5) = 'https '? 1: 0; $ ch = curl_init (); if (! Empty ($ data) {$ data = is_array ($ data )? Http_build_query ($ data): $ data; $ url. = (strpos ($ url ,'? ')? '&':"? "). $ Data;} curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, 'Get '); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // whether to capture the redirected page curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt ($ ch, CURLOPT_TIMEOUT, $ timeout ); curl_setopt ($ ch, CURLOPT_POST, 0); // curl_setopt ($ ch, CURLOPT_PORT, $ port); curl_setopt ($ ch, CURLOPT_HTTPHE ADER, $ header); curl_setopt ($ ch, CURLOPT_REFERER, $ url); // curl_setopt ($ ch, CURLOPT_USERAGENT, self: url2useragent ($ url )); if ($ is_ssl) {curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); // skip the certificate check curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, true ); // check whether the SSL encryption algorithm exists from the certificate} $ result = array (); $ result ['result'] = curl_exec ($ ch ); $ result ['http _ Code'] = curl_getinfo ($ ch, CURLINFO_HTTP_CODE); if (0! = Curl_errno ($ ch) {$ result ['error'] = "error: \ n ". curl_error ($ ch);} curl_close ($ ch); return $ result ;}
I personally feel that it has little to do with the code. I don't know what the problem is. I hope you will be grateful for your advice.

Reply content:
Encapsulate a function that captures web pages with curl. it can be tested locally. after it is placed on the test server, if it is accessed and executed through a browser, the HTTP status code returned by the function returns 0 in most cases, error message 'error: name lookup timed out'. if it is an idol, 200 is returned successfully. if it is directly executed on the test server using the command line, 100% is successful. The code is as follows:
Static public function curlGet ($ url, $ data = array (), $ header = array (), $ timeout = 3, $ port = 80) {$ is_ssl = substr ($ url, 0, 5) = 'https '? 1: 0; $ ch = curl_init (); if (! Empty ($ data) {$ data = is_array ($ data )? Http_build_query ($ data): $ data; $ url. = (strpos ($ url ,'? ')? '&':"? "). $ Data;} curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, 'Get '); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // whether to capture the redirected page curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt ($ ch, CURLOPT_TIMEOUT, $ timeout ); curl_setopt ($ ch, CURLOPT_POST, 0); // curl_setopt ($ ch, CURLOPT_PORT, $ port); curl_setopt ($ ch, CURLOPT_HTTPHE ADER, $ header); curl_setopt ($ ch, CURLOPT_REFERER, $ url); // curl_setopt ($ ch, CURLOPT_USERAGENT, self: url2useragent ($ url )); if ($ is_ssl) {curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); // skip the certificate check curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, true ); // check whether the SSL encryption algorithm exists from the certificate} $ result = array (); $ result ['result'] = curl_exec ($ ch ); $ result ['http _ Code'] = curl_getinfo ($ ch, CURLINFO_HTTP_CODE); if (0! = Curl_errno ($ ch) {$ result ['error'] = "error: \ n ". curl_error ($ ch);} curl_close ($ ch); return $ result ;}
I personally feel that it has little to do with the code. I don't know what the problem is. I hope you will be grateful for your advice.

1. set the timeout value to a greater value;
2. restart the server;
3. Check If DNS is normal

Your server is exhausted

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.