php curl Send Request Instance Method

  • 2021-12-13 07:50:09
  • OfStack

cURL can use the syntax of URL to simulate browsers to transfer data, Because it is a mock browser, So it also supports multiple protocols, FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE and LDAP protocols can be well supported, including one: HTTPS authentication, HTTP POST method, HTTP PUT method, FTP upload, keyberos authentication, HTTP upload, proxy server, cookies, username/password authentication, download file breakpoint renew, upload file breakpoint renew, http proxy server pipeline, and even it supports IPv6, scoket5 proxy server, upload file to FTP server through http proxy server, etc.

Using PHP's cURL library, you can simply and effectively grab web pages. All you need to do is run a script, then analyze the web page you crawled, and then you can get the data you want in a programmatic way. Whether you want to get some data from a link, or take an XML file and import it into the database, even if it is simply to get the content of the web page, cURL is a powerful PHP library.

1. PHP Basic steps for establishing CURL request

①: Initialization

curl_init()

②: Set attributes

curl_setopt (). There is a long string of cURL parameters to set that specify the details of the URL request.

③: Execute and obtain results

curl_exec()

④: Release the handle

curl_close()

2. Examples:

php curl Send get Request:


// Initialization 

$curl = curl_init();

// Set the crawled url

curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com');

// Set the information of the header file to be output as a data stream 

curl_setopt($curl, CURLOPT_HEADER, 1);

// Sets that the obtained information is returned as a file stream instead of being directly output. 

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// Execute a command 

$data = curl_exec($curl);

echo curl_getinfo($curl,CURLINFO_HTTP_CODE); // Output request status code 

// Shut down URL Request 

curl_close($curl);

// Display the obtained data 

print_r($data);

php curl Send post Request:


// Initialization 

$curl = curl_init();

// Set the crawled url

curl_setopt($curl, CURLOPT_URL, 'http://localhost/test/test.php');

// Set the information of the header file to be output as a data stream 

curl_setopt($curl, CURLOPT_HEADER, 1);

// Sets that the obtained information is returned as a file stream instead of being directly output. 

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// Settings post Mode submission 

curl_setopt($curl, CURLOPT_POST, 1);

// Settings post Data 

$post_data = array(

  "title" => "1290800466",

  "content" => "3424243243"

);

//post Data submitted 

curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

// Execute a command 

$data = curl_exec($curl);

// Shut down URL Request 

curl_close($curl);

// Display the obtained data 

print_r($data);

test.php:


<?php

$title = $_REQUEST['title'];

$content = $_REQUEST['content'];

error_log($title."\n",3,'error.log');

error_log($content."\n",3,'error.log');

echo 'ok';

3. PHP cURL function:

The following contains a list of PHP cURL functions:

函数 描述
curl_close() 关闭1个cURL会话。
curl_copy_handle() 复制1个cURL句柄和它的所有选项。
curl_errno() 返回最后1次的错误号。
curl_error() 返回1个保护当前会话最近1次错误的字符串。
curl_escape() 返回转义字符串,对给定的字符串进行URL编码。
curl_exec() 执行1个cURL会话。
curl_file_create() 创建1个 CURLFile 对象。
curl_getinfo() 获取1个cURL连接资源句柄的信息。
curl_init() 初始化1个cURL会话。
curl_multi_add_handle() 向curl批处理会话中添加单独的curl句柄。
curl_multi_close() 关闭1组cURL句柄。
curl_multi_exec() 运行当前 cURL 句柄的子连接。
curl_multi_getcontent() 如果设置了CURLOPT_RETURNTRANSFER,则返回获取的输出的文本流。
curl_multi_info_read() 获取当前解析的cURL的相关传输信息。
curl_multi_init() 返回1个新cURL批处理句柄。
curl_multi_remove_handle() 移除curl批处理句柄资源中的某个句柄资源。
curl_multi_select() 等待所有cURL批处理中的活动连接。
curl_multi_setopt() 设置1个批处理cURL传输选项。
curl_multi_strerror() 返回描述错误码的字符串文本。
curl_pause() 暂停及恢复连接。
curl_reset() 重置libcurl的会话句柄的所有选项。
curl_setopt_array() 为cURL传输会话批量设置选项。
curl_setopt() 设置1个cURL传输选项。
curl_share_close() 关闭cURL共享句柄。
curl_share_init() 初始化cURL共享句柄。
curl_share_setopt() 设置1个共享句柄的cURL传输选项。
curl_strerror() 返回错误代码的字符串描述。
curl_unescape() 解码URL编码后的字符串。
curl_version() 获取cURL版本信息。

Returns TRUE on success or FALSE on failure.

1 Some status codes

1xx: Request received, continue processing

2xx: Operation Successfully Received, Parsed, Accepted

3xx: One step must be taken to complete this request

4xx: Request contains 1 incorrect syntax or cannot be completed

5xx: The server failed to execute a fully valid request

100-Customer must continue to make requests

101--Client requests server to convert HTTP protocol version upon request

200-The transaction was successful

201--Prompt for URL for new files

202-Acceptance and processing, but processing is not completed

203--Indeterminate or incomplete information returned

204--Request received, but return message is null

205--The server completes the request, and the user agent must reset the currently browsed file

206-The server has completed some users' GET requests

300-The requested resource can be obtained in multiple places

301--Delete Request Data

302--Request data found at another address

303-Additional URL or access methods are recommended for customers

304--The client has executed GET, but the file has not changed

305--The requested resource must be obtained from the address specified by the server

306-Code used in the previous version of HTTP, which is no longer used in the current version

307--Declare temporary deletion of requested resources

400--Bad requests, such as syntax errors

401--Request for authorization failed

402--Retain valid ChargeTo header response

403--Request not allowed

404--No files, queries, or URl found

405--User-defined method in Request-Line field not allowed

406--The requested resource is inaccessible according to the Accept drag sent by the user

407--Similar to 401, the user must first be authorized on the proxy server

408--The client did not complete the request within the hungry time specified by the user

409--Request cannot be completed for current resource state

410--This resource no longer exists on the server and there is no reference address for further steps

411--Server rejects user-defined Content-Length attribute request

412--One or more request header fields are incorrect in the current request

413--The requested resource is larger than the size allowed by the server

414--The requested resource URL is longer than the length allowed by the server

415--Request resource does not support request item format

416--The request contains the Range request header field, and there is no range indication value in the current request resource range. Request

It also does not contain the If-Range request header field

417-The server does not meet the expected value specified in the EN header field of the request Expect. If it is a proxy server, it may be that the lower level 1 server cannot meet the request

500--Internal error generated by server

501--Server does not support requested function

502-The server is temporarily unavailable, sometimes to prevent system overload

503-Server overloaded or suspended maintenance

504--Gateway overloaded, server using another gateway or service to respond to users, long wait time set

505--The server does not support or reject the version of HTTP specified in the branch request header

These are all the knowledge points of php curl sending requests introduced this time. Thank you for your support of this site.


Related articles: