Home Backend Development PHP Tutorial Introduction to curl requesting other interfaces in the php interface

Introduction to curl requesting other interfaces in the php interface

Jul 05, 2018 am 11:12 AM
php interface

This article mainly introduces the introduction of curl requesting other interfaces in the PHP interface. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

Today I encountered a need to write The requirements for curl are as follows:

System A (shopping system) and system B (answering system) of the same application are independent of each other, and all user data exists in the database of system A.

Now we are in an operation in system B. We need to verify in system B whether the person currently requesting is a member of this application. To verify whether he is a

member of this application, of course we need to get the current user's Compare the logo to the database, but the database is in system A. At that time, I thought of curl and drew a picture:

In fact, to put it bluntly, it is to simulate http requests. , because unlike the front-end, http requests can be initiated directly through ajax or other methods, and the back-end has to

simulate this request through curl in order to achieve the same effect as the front-end.

So Baidu searched for a PHP curl method and encapsulated it:

1

2

3

4

5

6

7

8

9

public static function curl_post($url,$array){

        $curl = curl_init();        //设置提交的url       

        curl_setopt($curl, CURLOPT_URL, $url);        //设置post方式提交

        curl_setopt($curl, CURLOPT_POST, 1);        //设置post数据       

        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($array));        //执行命令

        $data = curl_exec($curl);        //关闭URL请求       

        curl_close($curl);        //获得数据并返回

        return $data;

     }

Copy after login

Then I happily called it:

1

2

3

$array = array('open_id'=>$openid);

   $url = dr_var("verify");

   $res = $this->curl_post($url,$array);

Copy after login

Here is a verify interface that simulates requesting system A in system B interface to review the user identity, and then determines system B based on the review results

What the interface returns to the outside world.

When I finally ran the B system interface, I found that in addition to the return value of the B system interface, it was also mixed with the return value of the A system interface (verify).

It was very confusing for a while. I was puzzled. I looked around to see what was wrong with the return value of the verify interface. exit(json_encode(array("a"=>a))) didn't work, so I replaced it with

return array("a" =>a); Still didn’t work, so I changed it to var_dump(array("a"=>a)); and it still didn’t work! ! ! Still returns the return values ​​of the two interfaces.

Later, later, I debugged the encapsulated curl function sentence by sentence, and found that the value of the verify interface had been printed directly in the red letter above,

So Baidu keyword: curl is not direct Output; blah blah, a bunch of related information came out. I opened one at random and found the answer. It turned out that curl master

set a parameter:

1

//获取数据不直接输出curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

Copy after login

In the end, the problem was solved, and it can be regarded as a small knowledge point about the curl parameter setting of PHP~~~

The above is the entire content of this article, I hope it will be helpful to everyone's learning, more related Please pay attention to the PHP Chinese website for content!

Related recommendations:

How to verify the legality of IP in PHP

##How to implement simulated multiple inheritance in PHP

The above is the detailed content of Introduction to curl requesting other interfaces in the php interface. 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

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)

How to use php interface and ECharts to generate visual statistical charts How to use php interface and ECharts to generate visual statistical charts Dec 18, 2023 am 11:39 AM

In today's context where data visualization is becoming more and more important, many developers hope to use various tools to quickly generate various charts and reports so that they can better display data and help decision-makers make quick judgments. In this context, using the Php interface and ECharts library can help many developers quickly generate visual statistical charts. This article will introduce in detail how to use the Php interface and ECharts library to generate visual statistical charts. In the specific implementation, we will use MySQL

How to combine ECharts and php interface to realize dynamic update of statistical charts How to combine ECharts and php interface to realize dynamic update of statistical charts Dec 17, 2023 pm 03:47 PM

How to combine ECharts and PHP interfaces to implement dynamic updates of statistical charts Introduction: Data visualization plays a vital role in modern applications. ECharts is an excellent JavaScript chart library that can help us easily create various types of statistical charts. PHP is a scripting language widely used in server-side development. By combining ECharts and PHP interfaces, we can realize dynamic updating of statistical charts, so that charts can be automatically updated according to changes in real-time data. Book

How to display real-time statistical charts through ECharts and php interfaces How to display real-time statistical charts through ECharts and php interfaces Dec 17, 2023 pm 04:35 PM

How to display real-time statistical charts through ECharts and PHP interfaces. With the rapid development of the Internet and big data technology, data visualization has become an important part. As an excellent open source JavaScript data visualization library, ECharts can help us display various statistical charts simply and efficiently. This article will introduce how to display real-time statistical charts through ECharts and PHP interfaces, and provide relevant code examples. 1. Preparation Before starting, we need to do some preparations

What are SPL interfaces (e.g., Iterator, Countable, ArrayAccess) and why use them? What are SPL interfaces (e.g., Iterator, Countable, ArrayAccess) and why use them? Apr 04, 2025 am 12:01 AM

The SPL interface includes Iterator, Countable and ArrayAccess in PHP. 1. The Iterator interface makes the object traversable and defines the current(), key(), next(), rewind() and valid() methods. 2. The Countable interface allows the object to report the number of elements and defines the count() method. 3. The ArrayAccess interface allows objects to be accessed and modified like arrays, and defines offsetExists(), offsetGet(), offsetSet() and offsetUnset() methods. These interfaces improve code efficiency and maintainability.

In-depth understanding of the definition and use of PHP interfaces In-depth understanding of the definition and use of PHP interfaces Mar 24, 2024 am 08:45 AM

Deeply understand the definition and usage of PHP interfaces. PHP is a powerful server-side scripting language that is widely used in the field of web development. In PHP, interface is an important concept that can be used to define the specifications of a set of methods without caring about the specific implementation of the methods. This article will delve into the definition and use of PHP interfaces and provide specific code examples. 1. What is an interface? In object-oriented programming, an interface is an abstract concept that defines the specification of a set of methods, but has no specific

How to implement data verification and verification of statistical charts through ECharts and php interfaces How to implement data verification and verification of statistical charts through ECharts and php interfaces Dec 18, 2023 pm 02:13 PM

How to implement data verification and verification of statistical charts through ECharts and PHP interfaces. As the demand for data visualization increases, ECharts has become a very popular data visualization tool. As a common back-end scripting language, PHP is also widely used in web development. This article will introduce how to implement data verification and verification of statistical charts through ECharts and PHP interfaces, and provide specific code examples. First, we need to understand ECharts. ECharts is an open source software developed by Baidu

How to generate interactive statistical charts through the php interface and ECharts How to generate interactive statistical charts through the php interface and ECharts Dec 18, 2023 pm 01:07 PM

In modern applications, visualization of data is becoming more and more popular. Statistical charts are a great way to visualize data and can easily help users understand trends in data. ECharts is a powerful front-end chart framework that provides rich chart types and interactive functions. Php is a very popular backend language that makes it easy to generate dynamic content and interfaces. In this article, we will introduce how to use the PHP interface and ECharts to generate interactive statistical charts, and provide specific code examples. one,

How to use php interface and ECharts to implement data filtering and filtering of statistical charts How to use php interface and ECharts to implement data filtering and filtering of statistical charts Dec 17, 2023 pm 05:36 PM

How to use the PHP interface and ECharts to implement data screening and filtering of statistical charts requires specific code examples. In data visualization, using statistical charts is a common way to display data. In practical applications, data often needs to be screened and filtered to meet different needs. The PHP interface and ECharts are two widely used tools through which data filtering and filtering of statistical charts can be implemented. The following will use an example to demonstrate how to use the PHP interface and ECharts implementation

See all articles