Home Backend Development PHP Tutorial A simple example of using the php curl_setopt() function to capture web pages and POST data

A simple example of using the php curl_setopt() function to capture web pages and POST data

May 19, 2017 am 11:29 AM

The previous article explained to you the concept of the curl_setopt function in PHP. It is an extension library of PHP. Using the curl_setopt() function can easily and quickly crawl web pages (can be used for collection) , using it needs to be configured and enabled in php.ini.

extension=php_curl.dll
Copy after login

A simple example of using the php curl_setopt() function to capture web pages and POST data

Now you can use the php curl_setopt function, so let’s look at the first usage first:

1. A crawled web page A simple case:

The code is as follows:

<?php

// 创建一个新cURL资源  
$ch = curl_init();

// 设置URL和相应的选项  
curl_setopt($ch, CURLOPT_URL, "http://www.php.cn/");
curl_setopt($ch, CURLOPT_HEADER, false);

// 抓取URL并把它传递给浏览器  
curl_exec($ch);

//关闭cURL资源,并且释放系统资源  
curl_close($ch); 
?>
Copy after login

The above example code captures a domain name of http://www.php. cn/ web page, the code execution result is as follows:

A simple example of using the php curl_setopt() function to capture web pages and POST data


##2. POST data case:

When using curl, there will often be data interaction, so it is more important.

 <?php  
     $ch = curl_init();  
     /*在这里需要注意的是,要提交的数据不能是二维数组或者更高 
     *例如array(&#39;name&#39;=>serialize(array(&#39;tank&#39;,&#39;zhang&#39;)),&#39;sex&#39;=>1,&#39;birth&#39;=>&#39;20101010&#39;) 
     *例如array(&#39;name&#39;=>array(&#39;tank&#39;,&#39;zhang&#39;),&#39;sex&#39;=>1,&#39;birth&#39;=>&#39;20101010&#39;)这样会报错的*/ 
     $data = array(&#39;name&#39; => &#39;test&#39;, &#39;sex&#39;=>1,&#39;birth&#39;=>&#39;20101010&#39;);  
     curl_setopt($ch, CURLOPT_URL, &#39;http://localhost/mytest/curl/upload.php&#39;);  
     curl_setopt($ch, CURLOPT_POST, 1);  
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
     curl_exec($ch);  
     ?>
Copy after login
In the upload.php file, print_r($_POST); can use curl to grab the content Array output by upload.php ( [name] => test [sex] => ; 1 [birth] => 20101010 )

【Recommended related articles】

1.

php curl_setopt function concept and usage examples

2 .

Detailed explanation of usage examples of PHP curl_exec function

The above is the detailed content of A simple example of using the php curl_setopt() function to capture web pages and POST data. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles