NetWork Network component
Component Introduction
The Z-BlogPHP built-in NetWork component is a self-developed component that has been included since version 1.0. After years of development and refinement, you are welcome to use it.
The NetWork component consists of a Network factory class and 3 business components: Network__curl, Network__filegetcontents, and Network__fsockopen classes, which respectively correspond to three ways of accessing networks in PHP.
When using the Network factory class to create a network operation component, the default creation order is: if the curl extension exists, initialize Network__curl; if allow_url_fopen is enabled and fsockopen exists, initialize Network__fsockopen; and finally, return Network__filegetcontents.
Component Method Introduction
Factory Class Methods
Network::Create(): Creates and returns a new network operation component in the default order.
Network::Create('curl'): Creates and returns a new Network__curl network operation component.
Network::Create('fsockopen'): Creates and returns a new Network__fsockopen network operation component.
Network::Create('filegetcontents'): Creates and returns a new Network__filegetcontents network operation component.
Business Component Properties
responseText: Returns the data.
responseHeader: Returns the Http Header.
status: Returns the status code.
statusText: Returns the status code text.
Business Component Methods
open($Method, $Url): Opens the network component.
send($varBody = ''): Sends the network component connection.
setRequestHeader($bstrHeader, $bstrValue): Sets the request Header.
enableGzip(): Enables Gzip compression.
setMaxRedirs($n): Sets the maximum number of redirects.
addBinary($name, $entity): Adds binary file upload.
addText($name, $entity): Adds sending parameters.
setTimeOuts($resolveTimeout, $connectTimeout, $sendTimeout, $receiveTimeout): Sets timeouts.
getAllResponseHeaders(): Gets all response headers.
getResponseHeader($name): Gets a specific response header.
The following are methods newly added in 1.7.2:
getStatusCode(): Gets the response code.
getStatusText(): Gets the complete response header.
getReasonPhrase(): Gets the explanation of the response code.
getBody(): Gets the response body.
getHeaders: Gets the response header array.
getHeader($name): Gets a specific response header.
hasHeader($name): Checks if a specific response header exists.
Simple Usage Method
Example Code 1: Accessing a webpage using GET method
// Assume we are fetching so.com
$url = 'https://www.so.com/';
$http = Network::Create();
$http->open('GET', $url);
$http->enableGzip();// Enable gzip
$http->setTimeOuts(120, 120, 0, 0);// Set timeout
$http->send();
// Get the body after access
$txt = $http->responseText;
// Get the HTTP response code
$code = $http->status;// Normal access is 200Example Code 2: Accessing a webpage using POST method
// Assume we are submitting login to a page
$url = 'https://test/login.php';
$data = array();
$data['username'] = '神马';
$data['password'] = md5('都是浮云');
$http = Network::Create();
$http->open('POST', $url);
$http->enableGzip();// Enable gzip
$http->setTimeOuts(120, 120, 0, 0);// Set timeout
$http->send($data);
$txt = $http->responseText;
echo $txt;Example Code 3: Setting RequestHeader, User-Agent, and Cookie headers
$u = 'ZBlogPHP/173000';//user-agent
$c = ' name=' . urlencode('nobody');// cookies name is nobody
$c .= ';password=' . urlencode('123456');// cookies password is 123456
// Before sending, set the request headers
$http->setRequestHeader('User-Agent', $u);
$http->setRequestHeader('Cookie', $c);Advanced Usage Method
Example Code 4: Specifying the number of redirects
// The default number of redirects for the NetWork component is 0, meaning it stops upon encountering a 301 or 302.
$url = 'https://test/http302.php';
$http = Network::Create();
$http->open('GET', $url);
// Set the number of redirects to 5, meaning a maximum of 5 redirects are allowed, which can prevent infinite loops.
$http->setMaxRedirs(5);
$http->send();Example Code 5: Uploading files
$url = 'https://test/upload.php';
$http = Network::Create('curl');// Specify to use the curl component
$http->open('POST', $url);
$http->enableGzip();// Enable gzip
$http->setTimeOuts(120, 120, 0, 0);// Set timeout
$http->addBinary('file', 'D:/www/web.config');// Specify a file
$http->send();
echo $http->responseText;
// Note: The filegetcontents and fsockopen in versions below 1.7.2 had issues submitting to https websites with self-signed certificates. Version 1.7.2 has fixed this.Example Code 6: Special usage of Network__curl
$http = Network::Create('curl');// Specify to use the curl component
$http->open('GET', 'https://www.baidu.com/');
// As is well known, curl has many settings. NetWork exposes a curl object for settings. This must be done after open() and before send().
if (is_object($http->ch)) {
curl_setopt($http->ch, CURLOPT_URL, 'https://www.so.com/s');
}
$http->send();Article link: https://docs.zbp.cool/zblogdocs/zblogapp/16.html
Helpful?
2025-10-09 21:21:32