Home >Backend Development >PHP Tutorial >php curl_exec() function CURL method to get the return value
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 valueIn 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 = '//m.sbmmt.com';
$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
$url = '//m.sbmmt.com';
$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.
1.
Detailed explanation of usage examples of PHP curl_exec functionShare a solution when the PHP server does not support the php curl_exec functionGet the output information of the CURL request from the php curl_exec functionThe 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!