Home  >  Article  >  Backend Development  >  Tips on using PHP HTTP request function

Tips on using PHP HTTP request function

PHPz
PHPzOriginal
2023-06-17 10:56:351839browse

PHP, as a low-level language, is widely used in network development. In web development, http requests are an essential part. This article will introduce the commonly used http request functions in PHP, how to use them, and the corresponding techniques.

1. curl

curl is a very popular http request function, which supports various protocols, authentication methods, proxies and other functions. It is part of the PHP base installation package, so no installation is required to use it.

(1) Basic usage

Sample code:

$url = 'http://www.example.com';
$ch = curl_init(); // 初始化curl
curl_setopt($ch, CURLOPT_URL, $url); // 设置请求的URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将curl请求的结果保存到变量中,而不直接输出到屏幕
$res = curl_exec($ch); // 执行请求
curl_close($ch); // 关闭curl
echo $res;

In the above example, we first initialized a curl handle using the curl_init() function , and then call the curl_setopt() function to set two options:

  • CURLOPT_URL: Set the requested URL.
  • CURLOPT_RETURNTRANSFER: Set curl to save the result directly into a variable without printing it out.

Finally, we call curl_exec() to execute the request and use curl_close() to close the curl handle. The result returned by the request will be saved in the variable $res, and we can output it directly.

(2) POST request

Sometimes, we need to send an HTTP POST request. In curl, we can use the CURLOPT_POST and CURLOPT_POSTFIELDS options to send POST requests.

Sample code:

$url = 'http://www.example.com';
$post_data = array('key1' => 'value1', 'key2' => 'value2');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true); //设置为POST请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //设置POST参数
$res = curl_exec($ch);
curl_close($ch);
echo $res;

In the above code, we first set the URL and POST parameters of the request, and then set the CURLOPT_POST in the curl option to identify the POST request , and use CURLOPT_POSTFIELDS to set the POST parameters. Finally, we use curl to execute the request and output the results.

(3) Timeout setting

When using curl, we may encounter timeout problems. To solve this problem, curl provides CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT options.

  • CURLOPT_TIMEOUT: If the request times out, curl will stop waiting and exit immediately.
  • CURLOPT_CONNECTTIMEOUT: If curl takes longer than the number of seconds set by this option to establish a connection with the remote host, the connection is considered failed and curl will stop waiting and exit.

Sample code:

$url = 'http://www.example.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 超时时间设置为10秒
$res = curl_exec($ch);
curl_close($ch);
echo $res;

In the above code, we set the CURLOPT_TIMEOUT option in the curl option to 10 seconds, indicating that if the request timeout exceeds 10 seconds, curl will stop waiting and exit.

2. file_get_contents

In addition to curl, PHP also provides some other http request functions. The most basic of these is file_get_contents.

(1) GET request

Sample code:

$url = 'http://www.example.com';
$res = file_get_contents($url); // 直接获取URL的内容
echo $res;

In the above code, we use PHP’s built-in file_get_contents() function to directly obtain the URL content and output it.

(2) Timeout setting

When using the file_get_contents function, we may need to set the timeout. We can create a context stream through the stream_context_create() function and then pass it as the third parameter to the file_get_contents() function.

Sample code:

$url = 'http://www.example.com';
$context = stream_context_create(array('http' => array('timeout' => 10))); // 超时时间设置为10秒
$res = file_get_contents($url, false, $context);
echo $res;

In the above code, we create a context stream and set its timeout to 10 seconds. We then use the file_get_contents() function and pass the context stream to the function as the third parameter.

3. fsockopen

fsockopen is the function in PHP used to open a socket. We can use this to communicate directly with the server.

Sample code:

$host = 'www.example.com';
$port = 80;
$timeout = 10;
$fp = fsockopen($host, $port, $errno, $errstr, $timeout); // 和服务器建立连接

$request = "GET / HTTP/1.1
";
$request .= "Host: $host
";
$request .= "Connection: close

"; // 请求头
fwrite($fp, $request); // 发送请求
while (!feof($fp)) {
    echo fgets($fp, 1024); // 输出结果
}
fclose($fp); // 关闭连接

In the above code, we use the fsockopen() function to open a socket connection and establish a connection to port 80 of the host www.example.com. We then set a GET request header and write it to the server over the socket. Finally, we use a while loop to output the results and use the fclose() function to close the connection.

Summary

HTTP request is one of the foundations of web development. This article introduces the commonly used http request functions in PHP and how to use them. Understanding how to use these functions can help us implement the corresponding functions more easily and efficiently.

The above is the detailed content of Tips on using PHP HTTP request function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn