Home Backend Development PHP Tutorial PHP curl get post request usage example sharing

PHP curl get post request usage example sharing

Oct 20, 2017 am 09:23 AM
curl php post

1. Summary of CURL

I personally summarize the curl request into three steps

1. Create a curl handle (curl_init) and set the parameters (curl_setopt) (open the refrigerator)

  2. Execute the request (curl_exec), process the returned data (stuff the elephant in)

3. Close curl (curl_close), release all resources (close the refrigerator)

In fact, if the code looks complicated, the complexity may be in the logic of processing the returned data.

, CURL_SETOPT

Therefore, the name is thought, setOption sets parameters, of which there are more parameters. Here are just a few commonly used commonly used. If you need to view more parameters, click here, commonly, common Set UA, Cookie, https, etc.


##

bool curl_setopt          (   , int  ,  "User-Agent: ""Referer: " 禁止 cURL 验证对等证书(peer'
Copy after login

If you need to return the Header header, add it yourself


curl_setopt($curl, CURLOPT_HEADER, 1);
Copy after login

Determine Returned status code:


curl_getinfo(, CURLINFO_HTTP_CODE)
if(curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200')
Copy after login

The simple version of the GET request is as follows. Taking the request to Baidu as an example, only the most basic attributes are set:


 =, CURLOPT_URL, 'http://www.baidu.com', CURLOPT_HEADER, 1, CURLOPT_RETURNTRANSFER, 1 = curl_exec(();
?>
Copy after login

It is a little complicated to set up UA, Cookie, etc. Only SSL certificate verification is needed in https requests, but not in http requests. If you need to request a regular address, similar to https:/ /example.com/?id=$i, just modify the for loop.


<?php
class getRequest
{    
const sUA = &#39;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)&#39;;    
const sURL = &#39;https://www.baidu.com&#39;;    
const sCookie = &#39;fake if you want&#39;;    
function vInitRequest()
    {        $curl = curl_init();

        curl_setopt($curl, CURLOPT_HEADER, self::sUA);
        curl_setopt($curl, CURLOPT_COOKIE, self::sCookie);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);        
        /*
         * ssl check,use for https url         
         */
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);//        
        for ($iId = 1; $iId < 1000; $iId++) {
//            $sURL = self::sURL.$iId;
        curl_setopt($curl, CURLOPT_URL, self::sURL);        
        $this->sExecRequest($curl);//        }    }    function sExecRequest($curl)
    {        $sRet = curl_exec($curl);        print_r($sRet);        /**
         * handle your response
         * stripos or preg         */
        curl_close($curl);
    }
}$foo = new getRequest();$foo->vInitRequest();?>
Copy after login

3. Separate the Header and Body in the Response

First, you need to set the Header information to display. You can get it by setting it as follows Header and body, of course, there are other methods that are similar


curl_setopt($curl, CURLOPT_HEADER, 1);list($sHeader, $sBody) = explode("\r\n\r\n", $sRet, 2);
Copy after login

Complete code:


<?php
class getRequest
{    
const sUA = &#39;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)&#39;;    
const sURL = &#39;https://www.baidu.com&#39;;    
const sCookie = &#39;fake if you want&#39;;    
function vInitRequest()
    {        
    $curl = curl_init();        
    $i = 0;
        curl_setopt($curl, CURLOPT_HEADER, self::sUA);
        curl_setopt($curl, CURLOPT_COOKIE, self::sCookie);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($curl, CURLOPT_URL, self::sURL);        
        $this->sExecRequest($curl);
    }    function sExecRequest($curl)
    {        $sRet = curl_exec($curl);        
    // if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == &#39;200&#39;) {
            list($sHeader, $sBody) = explode("\r\n\r\n", $sRet, 2);        
            // }
            print_r($sHeader);            
            print_r($sBody);        
            // curl_close($curl);    }
}$foo = new getRequest();$foo->vInitRequest();?>
Copy after login

4. POST request

The POST request simply sets two more parameters than the above Get request.

1. Hey, I want to submit data using POST.

2. The content of the data I POST


curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(&#39;user&#39;=>&#39;test&#39;));
Copy after login

The simple version is as follows:


<?php$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, &#39;http://www.baidu.com&#39;);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);$aPostData = array(  &#39;username&#39; => &#39;test&#39;,
   .....);
curl_setopt($curl, CURLOPT_POSTFIELDS, $aPostData);$sData = curl_exec($curl);
curl_close($curl);var_dump($sData);?>
Copy after login

The above is the detailed content of PHP curl get post request usage example sharing. 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