Home Backend Development PHP Tutorial php curl_exec() function CURL method to get the return value

php curl_exec() function CURL method to get the return value

May 18, 2017 am 10:45 AM

There is a parameter in CURL CURLOPT_RETURNTRANSFER: This parameter returns the information obtained by curl_exec() in the form of a file stream instead of outputting it directly. For example: The function of the CURLOPT_RETURNTRANSFER parameter is to

assign the content obtained by CRUL to a variable. It defaults to 0 and directly returns the text stream of the obtained output. Sometimes, it would not be good if we want to use the return value for judgment or other purposes. Therefore, sometimes we want the returned content to be stored in variables instead of being output directly, so what should we do? This article mainly introduces the method of

php curl_exec() function CURL to obtain the return value

In fact, CURLOPT_RETURNTRANSFER can be set. If it is set to CURLOPT_RETURNTRANSFER 1, it will use PHP curl to obtain the page content or Submit data and store it as a variable instead of outputting it directly.

1

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Copy after login

Let’s look at two examples below,

1. Curl gets the page content and directly outputs the example:

1

2

3

4

5

6

7

8

9

<strong><?php

$url = &#39;http://www.php.cn&#39;;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

curl_exec($ch);

curl_close($ch);

?></strong>

Copy after login
Run the code and you will find The obtained cul content will be output directly.

php curl_exec() function CURL method to get the return value

2. Curl gets the page content without directly outputting the example:

1

2

3

4

5

6

7

8

9

10

11

<strong><?php

$url = &#39;http://www.php.cn&#39;;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch); // 已经获取到内容,没有输出到页面上。

curl_close($ch);

echo $response;

?></strong>

Copy after login
When we set CURLOPT_RETURNTRANSFER to 1, The page has no output content. We assign the obtained content to a variable $response and use the variable $response output by echo.

php curl_exec() function CURL method to get the return value

【Recommended related articles】

1.

Detailed explanation of usage examples of PHP curl_exec function

2.

Share a solution when the PHP server does not support the php curl_exec function

3.

Get the output information of the CURL request from the php curl_exec function

The above is the detailed content of php curl_exec() function CURL method to get the return value. 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 Article Tags

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 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

How to realize the mutual conversion between CURL and python requests in python

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

Tutorial on updating curl version under Linux!

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

Three ways to get thread return value in Python Three ways to get thread return value in Python Apr 13, 2023 am 10:43 AM

Three ways to get thread return value in Python

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 to make HTTP requests

How to solve the problem that scanf return value is ignored How to solve the problem that scanf return value is ignored Nov 14, 2023 am 10:01 AM

How to solve the problem that scanf return value is ignored

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

what is linux curl

Use Java's Math.min() function to compare the size of two numbers and return the smaller value Use Java's Math.min() function to compare the size of two numbers and return the smaller value Jul 25, 2023 pm 01:21 PM

Use Java's Math.min() function to compare the size of two numbers and return the smaller value

See all articles