Home  >  Article  >  Backend Development  >  php curl_exec() function CURL method to get the return value

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

怪我咯
怪我咯Original
2017-05-18 10:45:5030922browse

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.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Let’s look at two examples below,

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

<?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);
?>
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:

<?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;
?>
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:
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