<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>cURL Archives - AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</title>
	<atom:link href="https://atcodex.com/tag/curl/feed/" rel="self" type="application/rss+xml" />
	<link>https://atcodex.com/tag/curl/</link>
	<description>Achieve financial success without sacrificing well-being! AtCodex provides actionable strategies for managing money, time, and personal growth—all in one place.</description>
	<lastBuildDate>Sat, 28 Aug 2021 17:14:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://atcodex.com/wp-content/uploads/2020/05/cropped-New-Project-1-32x32.png</url>
	<title>cURL Archives - AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</title>
	<link>https://atcodex.com/tag/curl/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to use curl in PHP</title>
		<link>https://atcodex.com/php/how-to-use-curl-in-php/</link>
		
		<dc:creator><![CDATA[atcodex]]></dc:creator>
		<pubDate>Wed, 30 Sep 2020 13:38:32 +0000</pubDate>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cURL]]></category>
		<guid isPermaLink="false">https://atcodex.com/?p=1068</guid>

					<description><![CDATA[<p>In this article, we are going to see how to How to use curl in PHP. There are many ways to fetch and post the data on the webpage. Normally &#8230; </p>
<p>The post <a href="https://atcodex.com/php/how-to-use-curl-in-php/">How to use curl in PHP</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">In this article, we are going to see how to How to use curl in PHP. There are many ways to fetch and post the data on the webpage. Normally we pass the data on the webpage using form submission, internal functions, and APIs. But sometimes we need to send the data from one server to another server. In that case, PHP provides us <strong>cURL</strong> built-in PHP extension.</p>



<p class="wp-block-paragraph"><strong>cURL </strong>is an extension that you can use to make various requests using different protocols from one server to another and on the same server also PHP has the default option to use cURL, and in this article, we’ll show you several examples related to this.</p>



<p class="wp-block-paragraph">When we work on some application, it is not necessary to have all the data in one database, sometime we use third party API or data in our web application. Most of the time we do things using JSON APIs But we can do the same thing using cURL which is secure and considered a good option to post and get the data.</p>



<p class="wp-block-paragraph">Let&#8217;s suppose you have two applications and you want to pass the data from one application to another. Then you have two options to do the same. The first one is via API and the second is via cURL. But what if you don&#8217;t want to display your API URL to the end-user in the developer console. In that case, you can post and get the data using curl, and in that way, in the front of the website, no one can know from where the data is coming. Otherwise, if you use API then simply anyone can check the data source in-browser developer console. </p>



<p class="wp-block-paragraph">The module for PHP that makes it possible for PHP programs to access curl functions within PHP. Before using the cURL in the application please make sure it is installed in the server. You can check it via phpinfo() function that will display in its output. </p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">PHP cURL Basics</h2>



<p class="wp-block-paragraph">Some basic cURL functions which are commonly used during cURL integration.</p>



<pre class="wp-block-code"><code>curl_init();      // initializes a cURL session
curl_setopt();    // changes the cURL session behavior with options
curl_exec();      // executes the started cURL session
curl_close();     // closes the cURL session and deletes the variable made by </code></pre>



<h2 class="wp-block-heading">Use of curl</h2>



<p class="wp-block-paragraph"><a href="https://www.php.net/manual/en/book.curl.php" target="_blank" rel="noreferrer noopener"><strong>cURL</strong> </a>is a PHP extension, that allows us to receive and send information via the URL syntax. By doing so, cURL makes it easy to communicate between different websites and domains without exposing the required URL data to the end-user.</p>



<h2 class="wp-block-heading">PHP curl post Request example</h2>



<p class="wp-block-paragraph">A POST request is usually made to send user collected data to a server. We have tried to make it easy with the below example so that you can understand it easily. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

$postRequest = array(
    'first' => 'atcodex',
    'second' => 'com'
);

$cURLConnection = curl_init('http://domainName.com/api');  // URL where you want to send the data
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

</pre>



<h2 class="wp-block-heading">PHP curl get Request example</h2>



<p class="wp-block-paragraph">A GET request retrieves data from a server. This can be a website’s HTML, an API response, or other resources.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

$cURLConnection = curl_init();

curl_setopt($cURLConnection, CURLOPT_URL, 'http://domainName.com/api?name=atcodex&amp;value=1');
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$phoneList = curl_exec($cURLConnection);
curl_close($cURLConnection);

</pre>



<h2 class="wp-block-heading">PHP cURL set custom Header</h2>



<p class="wp-block-paragraph">This is a guide on how to set custom request headers with PHP’s cuRL extension. Using cURL’s <strong>CURLOPT_HTTPHEADER</strong> option, we can change request headers such as Referer, Cookie, User-Agent, and Accept-Encoding.</p>



<p class="wp-block-paragraph">Take a look at the following cURL request:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">//The URL you're sending the request to.
$url = 'http://localhost/test/file.php';

//Create a cURL handle.
$ch = curl_init($url);

//Create an array of custom headers.
$customHeaders = array(
    'Accept-Encoding: gzip, deflate, br',
    'Cookie: PHPSESSID=ciqas3vp82m514iubpr1b0md3o;',
    'Referer: https://www.google.com/'
);

//Use the CURLOPT_HTTPHEADER option to use our
//custom headers.
curl_setopt($ch, CURLOPT_HTTPHEADER, $customHeaders);

//Set options to follow redirects and return output
//as a string.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

//Execute the request.
$result = curl_exec($ch);

echo $result;</pre>



<p class="wp-block-paragraph">In the PHP code snippet, we are sending a GET request to a URL. It is containing customer headers that we want to send with parameters.</p>



<p class="wp-block-paragraph">In this case, we set three custom headers using the <strong>CURLOPT_HTTPHEADER</strong> option:</p>



<ul class="wp-block-list"><li>We set the Accept-Encoding header to gzip, deflate, and br (Brotli).</li><li>We provided a PHPSESSID value as the <strong>Cookie</strong> header.</li><li>The HTTP referer header is set to the Google search engine.</li></ul>



<h2 class="wp-block-heading">Receive JSON POST Data using PHP</h2>



<p class="wp-block-paragraph">The following example shows how you can get or fetch the JSON POST data using PHP.</p>



<ul class="wp-block-list"><li>Use <strong>json_decode()</strong> function to decoded JSON data in PHP.</li><li>The <strong>file_get_contents()</strong> function is used to received data in a more readable format.</li></ul>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$data = json_decode(file_get_contents('php://input'), true);</pre>



<h2 class="wp-block-heading">Conclusion:</h2>



<p class="wp-block-paragraph">In the above post, we have tried to explain most of the useful things about the curl, its use, its purpose, and its benefit. cURL is good in case you need to call an API internally in an application without exposing data transfer URL between two or more domains to the end-user. cURL comes default with the PHP, but it is good to check before using in if it is available or not you can see <a href="https://atcodex.com/php/how-to-check-curl-is-available-using-php/" target="_blank" rel="noreferrer noopener">How to Check CURL is available using PHP</a> before using it in the application.</p>
<p>The post <a href="https://atcodex.com/php/how-to-use-curl-in-php/">How to use curl in PHP</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
