Home Backend Development PHP Tutorial Detailed analysis and problem summary of php using curl

Detailed analysis and problem summary of php using curl

Dec 23, 2016 pm 03:00 PM
curl

Today’s tool - CURL (Client URL Library) is introduced. Of course, today we will use this tool in PHP.

0. What is curl

1

PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.

Copy after login

This is an explanation of curl in PHP. Simply put, curl is a library that allows you to hook up, chat and communicate in depth with many different types of servers through URLs, and Many protocols are also supported. And people also said that curl can support https authentication, http post, ftp upload, proxy, cookies, simple password authentication and other functions.

After saying so much, I actually don’t feel much about it. I can only feel it in the application. At first, I needed to initiate a POST request to another server on the server side before I started to get in touch with curl, and then I felt it.

Before I officially talk about how to use it, let me mention that you must first install and enable the curl module in your PHP environment. I won’t go into the specific method. Different systems have different installation methods. You can check it on Google or check it out. The official PHP documentation is quite simple.

1. Try it first when you get it

After you get the tool, you need to play with it first and see if it is comfortable for you. Otherwise, if you use it as soon as you get it, you will mess up your own code and how can you mess with the server?

For example, let’s take Baidu, the famous “test network connection” website, as an example to try curl

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<?php

  // create curl resource

  $ch = curl_init();

  

  // set url

  curl_setopt($ch, CURLOPT_URL, "baidu.com");

  

  //return the transfer as a string

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  

  // $output contains the output string

  $output = curl_exec($ch);

  

  //echo output

  echo $output;

  

  // close curl resource to free up system resources

  curl_close($ch);  

?>

Copy after login

When you open this php file in the local environment browser, the page that appears is Baidu’s homepage, especially What about the "localhost" I just entered?

The above code and comments have fully explained what this code is doing.

$ch = curl_init(), creates a curl session resource, and returns a handle successfully;
curl_setopt($ch, CURLOPT_URL, "baidu.com"), sets the URL, needless to say;

The above two sentences can be combined Change $ch = curl_init("baidu.com");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0) This is to set whether to store the response result in a variable, 1 means to store it, 0 means to echo it out directly;

$ output = curl_exec($ch) executes, and then stores the response result in the $output variable for echo below;

curl_close($ch) closes this curl session resource.

Using curl in PHP is roughly in this form. The second step, setting parameters through the curl_setopt method is the most complicated and important. If you are interested, you can read the official detailed reference on settable parameters, which will help you. It makes me want to vomit, but practice makes perfect as needed.

To summarize, the usage of curl in PHP is: create curl session -> Configuration parameters -> Execute -> Close session.

Let’s take a look at some common scenarios. How do we need to “dress ourselves” (configuration parameters) to correctly “pick up girls” (correctly pick up the server).

2. Say hello - GET and POST requests and HTTPS protocol processing

Say hello to the server first, send a Hello to the server and see how she responds. The most convenient way here is to send a GET request to the server, of course Small notes like POST are also OK.

2.1 GET request

Let’s take “searching for keywords in a famous gay dating website github” as an example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

//通过curl进行GET请求的案例

<?php

  // create curl resource

  $ch = curl_init();

  

  // set url

  curl_setopt($ch, CURLOPT_URL, "https://github.com/search?q=react");

  

  //return the transfer as a string

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  

  // $output contains the output string

  $output = curl_exec($ch);

  

  //echo output

  echo $output;

  

  // close curl resource to free up system resources

  curl_close($ch);  

?>

Copy after login

It seems to be no different from the previous example, but here are 2 points that can be mentioned:

1 .The default request method is GET, so there is no need to explicitly specify the GET method;
2. https request, non-http request. Some people may have seen in various places that HTTPS requests need to add a few lines of code to bypass the SSL certificate check. The resource was successfully requested, but it seems that it is not needed here. What is the reason?

1

2

3

4

The two Curl options are defined as:

CURLOPT_SSL_VERIFYPEER - verify the peer&#39;s SSL certificate 

CURLOPT_SSL_VERIFYHOST - verify the certificate&#39;s name against host

They both default to true in Curl, and shouldn&#39;t be disabled unless you&#39;ve got a good reason. Disabling them is generally only needed if you&#39;re sending requests to servers with invalid or self-signed certificates, which is only usually an issue in development. Any publicly-facing site should be presenting a valid certificate, and by disabling these options you&#39;re potentially opening yourself up to security issues.

Copy after login

That is, unless an illegal or self-made certificate is used, which mostly occurs in the development environment, you should set these two lines to false to avoid the SSL certificate check. Otherwise, there is no need to do this, and it is not necessary to do so. Safe practice.

2.2 POST request

So how to make a POST request? For testing, first pass a script to receive POST on a test server:

1

2

3

4

5

//testRespond.php

<?php

  $phpInput=file_get_contents(&#39;php://input&#39;);

  echo urldecode($phpInput);

?>

Copy after login

Send normal data

Then write a request locally:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<?php

  $data=array(

  "name" => "Lei",

  "msg" => "Are you OK?"

  );

  

  $ch = curl_init();

  

  curl_setopt($ch, CURLOPT_URL, "http://测试服务器的IP马赛克/testRespond.php");

  curl_setopt($ch, CURLOPT_POST, 1);

  //The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.

  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);

  curl_setopt($ch, CURLOPT_POSTFIELDS , http_build_query($data));

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  

  $output = curl_exec($ch);

  

  echo $output;

  

  curl_close($ch);  

?>

Copy after login

The browser running result is:

name=Lei&msg=Are you OK ?

Here we construct an array and pass it to the server as POST data:

curl_setopt($ch, CURLOPT_POST, 1) indicates a POST request;
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60) sets the longest possible The enduring connection time is in seconds. You can’t wait forever and become a mummy;
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)) sets the data field of POST, because it is in the form of array data (will come later) Talking about json format), so use http_build_query to process it.

How to make a POST request for json data?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?php

  $data=&#39;{"name":"Lei","msg":"Are you OK?"}&#39;;

  

  $ch = curl_init();

  

  curl_setopt($ch, CURLOPT_URL, "http://测试服务器的IP马赛克/testRespond.php");

  curl_setopt($ch, CURLOPT_POST, 1);

  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);

  curl_setopt($ch, CURLOPT_HTTPHEADER, array(&#39;Content-Type: application/json&#39;, &#39;Content-Length:&#39; . strlen($data)));

  curl_setopt($ch, CURLOPT_POSTFIELDS , $data);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  

  $output = curl_exec($ch);

  

  echo $output;

  

  curl_close($ch);  

?>

Copy after login

The browser executes and displays:

{"name":"Lei","msg":"Are you OK?"}

3. How to upload and download files

has hooked up with the server, At this time, you have to ask for a photo to take a look. You also have to post your own photo for others to take a look at. Although the appearance of two people together is not important, a handsome man and a beautiful woman are always the best.

3.1 Send a photo of yourself to show your sincerity - POST upload file

Similarly to the remote server, we first send a receiving script to receive the picture and save it locally. Pay attention to file and folder permission issues, which need to be written Access permission:

1

2

3

4

5

6

7

8

9

10

<?php

  if($_FILES){

    $filename = $_FILES[&#39;upload&#39;][&#39;name&#39;];

     $tmpname = $_FILES[&#39;upload&#39;][&#39;tmp_name&#39;];

     //保存图片到当前脚本所在目录

     if(move_uploaded_file($tmpname,dirname(__FILE__).&#39;/&#39;.$filename)){

      echo (&#39;上传成功&#39;);

     }

  }

?>

Copy after login

然后我们再来写我们本地服务器的php curl部分:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?php

  $data = array(&#39;name&#39;=>&#39;boy&#39;, "upload"=>"@boy.png");

  

  $ch = curl_init();

  

  curl_setopt($ch, CURLOPT_URL, "http://远程服务器地址马赛克/testRespond.php");

  curl_setopt($ch, CURLOPT_POST, 1);

  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);

  curl_setopt($ch, CURLOPT_POSTFIELDS , $data);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  

  $output = curl_exec($ch);

  

  echo $output;

  

  curl_close($ch);    

?>

Copy after login

浏览器中运行一下,什么都米有,去看一眼远程的服务器,还是什么都没有,并没有上传成功。

为什么会这样呢?上面的代码应该是大家搜索curl php POST图片最常见的代码,这是因为我现在用的是PHP5.6以上版本,@符号在PHP5.6之后就弃用了,PHP5.3依旧可以用,所以有些同学发现能执行啊,有些发现不能执行,大抵是因为PHP版本的不同,而且curl在这两版本中实现是不兼容的,上面是PHP5.3的实现。

下面来讲PHP5.6及以后的实现,:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?php

  $data = array(&#39;name&#39;=>&#39;boy&#39;, "upload"=>"");

  $ch = curl_init();

  

  $data[&#39;upload&#39;]=new CURLFile(realpath(getcwd().&#39;/boy.png&#39;));

  

  curl_setopt($ch, CURLOPT_URL, "http://115.29.247.189/test/testRespond.php");

  curl_setopt($ch, CURLOPT_POST, 1);

  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);

  curl_setopt($ch, CURLOPT_POSTFIELDS , $data);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  

  $output = curl_exec($ch);

  

  echo $output;

  

  curl_close($ch);    

?>

Copy after login

这里引入了一个CURLFile对象进行实现,关于此的具体可查阅文档进行了解。这时候再去远程服务器目录下看看,发现有了一张图片了,而且确实是我们刚才上传的图片。

3.2 获取远程服务器妹子的照片 —— 抓取图片

服务器妹子也挺实诚的,看了照骗觉得我长得挺慈眉善目的,就大方得拿出了她自己的照片,但是有点害羞的是,她不愿意主动拿过来,得我们自己去取。

远程服务器在她自己的目录下存放了一个图片叫girl.jpg,地址是她的web服务器根目录/girl.jpg,现在我要去获取这张照片。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<?php

  $ch = curl_init();

  

  $fp=fopen(&#39;./girl.jpg&#39;, &#39;w&#39;);

  

  curl_setopt($ch, CURLOPT_URL, "http://远程服务器地址马赛克/girl.jpg");

  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);

  curl_setopt($ch, CURLOPT_FILE, $fp);

  

  $output = curl_exec($ch);

  $info = curl_getinfo($ch);

  

  fclose($fp);

  

  $size = filesize("./girl.jpg");

  if ($size != $info[&#39;size_download&#39;]) {

    echo "下载的数据不完整,请重新下载";

  } else {

    echo "下载数据完整";

  }

  

  curl_close($ch); 

?>

Copy after login

现在,在我们当前目录下就有了一张刚拿到的照片啦,是不是很激动呢!

这里值得一说的是curl_getinfo方法,这是一个获取本次请求相关信息的方法,对于调试很有帮助,要善用。

4. HTTP认证怎么搞

这个时候呢,服务器的家长说这个我们女儿还太小,不能找对象,就将她女儿关了起来,并且上了一个密码锁,所谓的HTTP认证,服务器呢偷偷托信鸽将HTTP认证的用户名和密码给了你,要你去见她,带她私奔。

那么拿到了用户名和密码,我们怎么通过PHP CURL搞定HTTP认证呢?

PS:这里偷懒就不去搭HTTP认证去试了,直接放一段代码,我们分析下。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

function curl_auth($url,$user,$passwd){

  $ch = curl_init();

  curl_setopt_array($ch, [

    CURLOPT_USERPWD => $user.&#39;:&#39;.$passwd,

    CURLOPT_URL   => $url,

    CURLOPT_RETURNTRANSFER => true

  ]);

  $result = curl_exec($ch);

  curl_close($ch);

  return $result;

}

  

$authurl = &#39;http://要请求HTTP认证的地址&#39;;

  

echo curl_auth($authurl,&#39;vace&#39;,&#39;passwd&#39;);

Copy after login

1

2

3

4

5

这里有一个地方比较有意思:

curl_setopt_array 这个方法可以通过数组一次性地设置多个参数,防止有些需要多处设置的出现密密麻麻的curl_setopt方法。

5.利用cookie模拟登陆

这时你成功见到了服务器妹子,想带她私奔,但是无奈没有盘缠走不远,服务器妹子说,她妈服务器上有金库,可以登陆上去搞一点下来。

首先我们先来分析一下,这个事情分两步,一是去登陆界面通过账号密码登陆,然后获取cookie,二是去利用cookie模拟登陆到信息页面获取信息,大致的框架是这样的。

Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<?php

 //设置post的数据

 $post = array (

  &#39;email&#39; => &#39;账户&#39;,

  &#39;pwd&#39; => &#39;密码&#39;

 );

 //登录地址

 $url = "登陆地址";

 //设置cookie保存路径

 $cookie = dirname(__FILE__) . &#39;/cookie.txt&#39;;

 //登录后要获取信息的地址

 $url2 = "登陆后要获取信息的地址";

 //模拟登录

 login_post($url, $cookie, $post);

 //获取登录页的信息

 $content = get_content($url2, $cookie);

 //删除cookie文件

 @ unlink($cookie);

  

 var_dump($content); 

?>

Copy after login

然后我们思考下下面两个方法的实现:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

login_post($url, $cookie, $post)

get_content($url2, $cookie)

//模拟登录

function login_post($url, $cookie, $post) {

  $curl = curl_init();

  curl_setopt($curl, CURLOPT_URL, $url);

  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);

  curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);

  curl_setopt($curl, CURLOPT_POST, 1);

  curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));

  curl_exec($curl);

  curl_close($curl);

}

//登录成功后获取数据

function get_content($url, $cookie) {

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);

  curl_setopt($ch, CURLOPT_HEADER, 0);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);

  $rs = curl_exec($ch);

  curl_close($ch);

  return $rs;

}

Copy after login

至此,总算是模拟登陆成功,一切顺利啦,通过php CURL“撩”服务器就是这么简单。

当然,CURL的能力远不止于此,本文仅希望就后端PHP开发中最常用的几种场景做一个整理和归纳。最后一句话,具体问题具体分析。

更多php使用curl详细解析及问题汇总相关文章请关注PHP中文网!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
24
How to realize the mutual conversion between CURL and python requests in python How to realize the mutual conversion between CURL and python requests in python May 03, 2023 pm 12:49 PM

Both curl and Pythonrequests are powerful tools for sending HTTP requests. While curl is a command-line tool that allows you to send requests directly from the terminal, Python's requests library provides a more programmatic way to send requests from Python code. The basic syntax for converting curl to Pythonrequestscurl command is as follows: curl[OPTIONS]URL When converting curl command to Python request, we need to convert the options and URL into Python code. Here is an example curlPOST command: curl-XPOST https://example.com/api

PHP8.1 released: Introducing curl for concurrent processing of multiple requests PHP8.1 released: Introducing curl for concurrent processing of multiple requests Jul 08, 2023 pm 09:13 PM

PHP8.1 released: Introducing curl for concurrent processing of multiple requests. Recently, PHP officially released the latest version of PHP8.1, which introduced an important feature: curl for concurrent processing of multiple requests. This new feature provides developers with a more efficient and flexible way to handle multiple HTTP requests, greatly improving performance and user experience. In previous versions, handling multiple requests often required creating multiple curl resources and using loops to send and receive data respectively. Although this method can achieve the purpose

From start to finish: How to use php extension cURL to make HTTP requests From start to finish: How to use php extension cURL to make HTTP requests Jul 29, 2023 pm 05:07 PM

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

Tutorial on updating curl version under Linux! Tutorial on updating curl version under Linux! Mar 07, 2024 am 08:30 AM

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

How to handle 301 redirection of web pages in PHP Curl? How to handle 301 redirection of web pages in PHP Curl? Mar 08, 2024 am 11:36 AM

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

what is linux curl what is linux curl Apr 20, 2023 pm 05:05 PM

In Linux, curl is a very practical tool for transferring data to and from the server. It is a file transfer tool that uses URL rules to work under the command line; it supports file upload and download, and is a comprehensive transfer tool. . Curl provides a lot of very useful functions, including proxy access, user authentication, ftp upload and download, HTTP POST, SSL connection, cookie support, breakpoint resume and so on.

How to set cookies in php curl How to set cookies in php curl Sep 26, 2021 am 09:27 AM

How to set cookies in php curl: 1. Create a PHP sample file; 2. Set cURL transmission options through the "curl_setopt" function; 3. Pass the cookie in CURL.

Solution to PHP Fatal error: Call to undefined function curl_setopt() Solution to PHP Fatal error: Call to undefined function curl_setopt() Jun 23, 2023 am 08:18 AM

PHP is a widely used open source scripting language used by many websites. However, sometimes you may encounter the problem PHPFatalerror:Calltoundefinedfunctioncurl_setopt(), which may prevent your website from working properly. So what exactly causes this problem? In PHP, curl_setopt() is a very important function, which is used to extend the library through curl

See all articles