PHP Curl POST and GET Methods

Today, We want to share with you PHP Curl POST and GET Methods.In this post we will show you how to get curl post data in php, hear for how to fetch data using curl in php we will give you demo and example for implement.In this post, we will learn about php curl get request with parameters with an example.

PHP Curl POST and GET Methods

There are the Following The simple About PHP Curl POST and GET Methods Full Information With Example and source code.

As I will cover this Post with live Working example to develop Submitting a form post with PHP and CURL, so the cURL API calls with PHP and json data (GET – POST – PUT – DELETE) for this example is following below.

PHP Curl GET Method

Below is simple function for sending GET request using Curl. We send search request to your domain and get as return some matching query results.

$url = 'https://www.your_api_domain_name.com/search';

    $data = array (
        'q' => 'mobile'
        );
        
        $parameters = '';
    foreach($data as $key=>$value)
                $parameters .= $key.'='.$value.'&';
         
        $parameters = trim($parameters, '&');

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url.'?'.$parameters );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 7); 
    curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    
        $result = curl_exec($ch);
    curl_close($ch);

if(curl_errno($ch))
  echo 'Curl error: ' . curl_error($ch);
else
  echo $result;

PHP Curl POST Method

and To send PHP server side POST request using curl to specific curl remote location we will update above source code and Include some extra PHP curl options.

$url = 'http://www.your_api_domain_name.com';

    $data = array (
        'product_id' => 'P0015856',
        'product_app_key' => '565ddddF5665dfdD4DF5',
        'access_data' => 'main'
        );
        
	$parameters = '';
	foreach($data as $key=>$value)
			$parameters .= $key.'='.$value.'&';
         
    $parameters = trim($parameters, '&');

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 7); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt($ch, CURLOPT_HEADER, 0);
        

    curl_setopt($ch, CURLOPT_POST, count($data));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); //parameters data
    
    $result = curl_exec($ch);
    curl_close($ch);

    echo $result;

PUT vs. POST in REST

PHP Curl POST and PUT diff
PHP Curl POST and PUT diff
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Curl POST and GET Methods.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment