php调用远路url的六种方法
php调用远程url的六种方法
示例代码1: 用file_get_contents 以get方式获取内容
???
$url='http://www.baidu.com/';
$html = file_get_contents($url);
?? //print_r($http_response_header);
?? ec($html);
?? printhr();
?? printarr($http_response_header);
printhr();
?? ?>
?
??? 示例代码2: 用fopen打开url, 以get方式获取内容
??
?? $fp = fopen($url, 'r');
? ?
?? printarr(stream_get_meta_da
printhr();
?? while(!feof($fp)) {
?????? $result .= fgets($fp, 1024);
}
echo "url body:??? $result";
?? printhr();
?? fclose($fp);
?? ?>
示例代码3:用file_get_contents函数,以post方式获取url
?? ?? $data = array ('foo' => 'bar');
?? $data = http_build_query($data);
?? $opts = array (
'http' => array (
?? 'method' => 'POST',
??? 'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
???? "Content-Length: " . strlen($data) . "\r\n",
?? 'content' => $data
?? ),
?? );
$context = stream_context_create($opts);
$html = file_get_contents('http://localhost/e/admin/test.html', false, $context);
echo $html;
?>
示例代码4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body
function get_url ($url,$cookie=false) {
$url = parse_url($url);
$query = $url[path]."?".$url[query];
ec("Query:".$query);
$fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
if (!$fp) {
?????? return false;
} else {
?????? $request = "GET $query HTTP/1.1\r\n";
?????? $request .= "Host: $url[host]\r\n";
?????? $request .= "Connection: Close\r\n";
?????? if($cookie) $request.="Cookie:?? $cookie\n";
?????? $request.="\r\n";
?????? fwrite($fp,$request);
?????? while([email protected]($fp)) {
???????? $result .= @fgets($fp, 1024);
?????? }
?????? fclose($fp);
?????? return $result;
}
}
//获取url的html部分,去掉header
function GetUrlHTML($url,$cookie=false) {
??? $rowdata = get_url($url,$cookie);
??? if($rowdata)
??? {
??????? $body= stristr($rowdata,"\r\n\r\n");
??????? $body=substr($body,4,strlen($body));
??????? return $body;
??? }
? ?
??? return false;
}
?>
示例代码5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body
function HTTP_Post($URL,$data,$cookie, $referrer="") {
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer=="") // if not given use this script as referrer
$referrer="111";
// making string from $data
foreach($data as $key=>$value)
$values[]="$key=".urlencode($value);
$data_string=implode("&",$values);
// Find out which port is needed - if not given use standard (=80)
if(!isset($URL_Info["port"]))
$URL_Info["port"]=80;
// building POST-request:
$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
$request.="Host: ".$URL_Info["host"]."\n";
$request.="Referer: $referer\n";
$request.="Content-type: application/x-www-form-urlencoded\n";
$request.="Content-length: ".strlen($data_string)."\n";
$request.="Connection: close\n";
$request.="Cookie:?? $cookie\n";
$request.="\n";
$request.=$data_string."\n";
$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
fputs($fp, $request);
while(!feof($fp)) {
?????? $result .= fgets($fp, 1024);
}
fclose($fp);
return $result;
}
printhr();
?>
示例代码6:使用curl库,使用curl库之前,你可能需要查看一下php.ini,查看是否已经打开了curl扩展
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.baidu.com/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
关于curl库:
curl官方网站http://curl.haxx.se/
curl是使用URL语法的传送文件工具,支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICTFILE和LDAP。curl 支持SSL证书、HTTP POST、HTTP PUT 、FTP上传,kerberos、基于HTT格式的上传、代理、cookie、用户+口令证明、文件传送恢复、http代理通道和大量其他有用的技巧
function printarr(array $arr)
{
??? echo "
Row field count: ".count($arr)."
";
??? foreach($arr as $key=>$value)
??? {
????? ?
??????????? echo "$key=$value???
";
??? }
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

Differences: 1. Different definitions, url is a uniform resource locator, and html is a hypertext markup language; 2. There can be many urls in an html, but only one html page can exist in a url; 3. html refers to is a web page, and url refers to the website address.

How to handle 301 redirection of web pages in PHPCurl? When using PHPCurl to send network requests, you will often encounter a 301 status code returned by the web page, indicating that the page has been permanently redirected. In order to handle this situation correctly, we need to add some specific options and processing logic to the Curl request. The following will introduce in detail how to handle 301 redirection of web pages in PHPCurl, and provide specific code examples. 301 redirect processing principle 301 redirect means that the server returns a 30

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

The HTTP request times out, and the server often returns the 504GatewayTimeout status code. This status code indicates that when the server executes a request, it still fails to obtain the resources required for the request or complete the processing of the request after a period of time. It is a status code of the 5xx series, which indicates that the server has encountered a temporary problem or overload, resulting in the inability to correctly handle the client's request. In the HTTP protocol, various status codes have specific meanings and uses, and the 504 status code is used to indicate request timeout issues. in customer

Common problems and solutions for cookie settings, specific code examples are required. With the development of the Internet, cookies, as one of the most common conventional technologies, have been widely used in websites and applications. Cookie, simply put, is a data file stored on the user's computer that can be used to store the user's information on the website, including login name, shopping cart contents, website preferences, etc. Cookies are an essential tool for developers, but at the same time, cookie settings are often encountered
