Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use curl_exec function in php

Shulou Source: shulou.com Published: 2022-06-03 11:02:25 11月15日 Update

Editor to share with you how to use the curl_exec function in php, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

There is a parameter CURLOPT_RETURNTRANSFER in CURL: this parameter returns the information obtained by curl_exec () as a file stream rather than directly outputting it. For example, the function of the CURLOPT_RETURNTRANSFER parameter is to assign the content obtained by CRUL to the variable. It defaults to 0, which directly returns the text stream of the obtained output, and sometimes it is not good for us to use the return value for judgment or other purposes. So, sometimes we want the returned content to be stored as a variable rather than output directly, so what should we do?

The method of getting the return value of phpcurl_exec () function CURL

In fact, CURLOPT_RETURNTRANSFER can be set, if set to CURLOPT_RETURNTRANSFER1, it will use PHPcurl to get the page content or submit data, stored as a variable, rather than direct output.

Curl_setopt ($ch,CURLOPT_RETURNTRANSFER,1)

Example of the phpcurl_exec () function

1. Curl acquires the content of the page and outputs examples directly:

$url=' http://www.php.cn';

$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 that the acquired cul content will be output directly.

2. Curl acquires the content of the page without directly outputting examples:

$url=' http://www.php.cn';

$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); / / the content has been obtained and not output to the page.

Curl_close ($ch)

Echo$response

? >

When we set CURLOPT_RETURNTRANSFER to 1, the page has no output, and we assign the acquired content to a variable $response, the variable $response output with echo.

The above is all the contents of the article "how to use the curl_exec function in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Content output variables pages functions parameters articles examples time not much code functions information most forms data files text methods more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info NVidia Microsoft MariaDB Huawei