⊗ppSpCrRV 30 of 83 menu

Storing CURL Response in a Variable in PHP

The following setting forces the server response to be saved into a variable instead of being output to the browser page:

<?php curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); ?>

Let's adjust the code according to this setting:

<?php // Page URL for the request: $url = 'http://test.loc'; // Initialize the session: $curl = curl_init(); // Specify the page URL: curl_setopt($curl, CURLOPT_URL, $url); // Save the server response to a variable: curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // Execute the request, saving the response to a variable: $res = curl_exec($curl); // See what's in the variable: var_dump($res); ?>

Wrap the provided code into a function. Let this function accept a URL as a parameter, and return the received result.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline