Tuesday, November 14, 2017

phpMyPassion

Send Get / Post Data Using PHP CURL

In this tutorial, I am explaining How to send Get / Post PHP  curl Request with Parameters .  

I am explaining 3 types of curl request with an example of S2S communication.


#PHP Simple cUrl  Request Without Parameter.

You can use below code to send CURL request with parameter.

<?php 
$AffPostbackUrl = 'http://phpmypassion.com';
curl_request($AffPostbackUrl);
function curl_request($url, array $options = array()) 
{ 
    $defaults = array( 
        CURLOPT_POST => 0, 
        CURLOPT_HEADER => 0, 
        CURLOPT_URL => $url, 
        CURLOPT_FRESH_CONNECT => 1, 
        CURLOPT_RETURNTRANSFER => 0, 
        CURLOPT_FORBID_REUSE => 1, 
        CURLOPT_TIMEOUT => 4 
    ); 
    $ch = curl_init(); 
    curl_setopt_array($ch, ($options + $defaults)); 
    if( ! $result = curl_exec($ch)) 
    { 
        trigger_error(curl_error($ch)); 
    }
    curl_close($ch); 
    return $result; 
} 

#PHP cUrl  Request With Get Method.

You can use below code to send CURL request with parameter.

<?php 
$AffPostbackUrl = 'http://phpmypassion.com';
$get =  array('click'=> '1234');
curl_get($AffPostbackUrl,$get);
function curl_get($url, array $get = NULL, array $options = array()) 
{ 
    $defaults = array( 
        CURLOPT_POST => 1, 
        CURLOPT_HEADER => 0, 
        CURLOPT_URL => $url, 
        CURLOPT_FRESH_CONNECT => 1, 
        CURLOPT_RETURNTRANSFER => 0, 
        CURLOPT_FORBID_REUSE => 1, 
        CURLOPT_TIMEOUT => 4, 
        CURLOPT_POSTFIELDS => http_build_query($get) 
    ); 
    //echo '<pre>';
//print_r($defaults);
    $ch = curl_init(); 
    curl_setopt_array($ch, ($options + $defaults)); 
    if( ! $result = curl_exec($ch)) 
    { 
        trigger_error(curl_error($ch)); 
    } 
    //echo 'dsjjkdf='.$result;
    curl_close($ch); 
    return $result; 
} 

#PHP cUrl  Request With Post Method.


You can use below code to send CURL request with parameter in post method.

<?php 
$AffPostbackUrl = 'http://phpmypassion.com';
$post =  array('click'=> '1234');
curl_post($AffPostbackUrl,$post);
function curl_post($url, array $post = NULL, array $options = array()) 
{ 
    $defaults = array( 
        CURLOPT_POST => 1, 
        CURLOPT_HEADER => 0, 
        CURLOPT_URL => $url, 
        CURLOPT_FRESH_CONNECT => 1, 
        CURLOPT_RETURNTRANSFER => 0, 
        CURLOPT_FORBID_REUSE => 1, 
        CURLOPT_TIMEOUT => 4, 
        CURLOPT_POSTFIELDS => http_build_query($post) 
    ); 
    //echo '<pre>';
//print_r($defaults);
    $ch = curl_init(); 
    curl_setopt_array($ch, ($options + $defaults)); 
    if( ! $result = curl_exec($ch)) 
    { 
        trigger_error(curl_error($ch)); 
    } 
    //echo 'dsjjkdf='.$result;
    curl_close($ch); 
    return $result; 
} 

You can test your code with below editor.



About Author -

Hi, I am Anil.

Welcome to my eponymous blog! I am passionate about web programming. Here you will find a huge information on web development, web design, PHP, Python, Digital Marketing and Latest technology.

Subscribe to this Blog via Email :

Note: Only a member of this blog may post a comment.