Powered By Blogger
Showing posts with label cURL. Show all posts
Showing posts with label cURL. Show all posts

Thursday, September 13, 2018

API call via cURL in php

$api_data = array(
'post' => $post
);

$api_data_json = json_encode($api_data);

$base_url = home_url().'wp-json/gic/v1/subscriber';

$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $api_data_json);

curl_setopt($curl, CURLOPT_URL, $base_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
        if(!$result){die("Connection Failure");}
        curl_close($curl);




https://www.weichieprojects.com/blog/curl-api-calls-with-php/

Thursday, February 9, 2017

cURL in drupal 8 OR webservice in drupal

https://www.drupal.org/node/1838208


I have used both with good results drupal_http_request() is somewhat simpler to use in my opinion but cURL provides more options.
Here are two sample examples for a custom module.
drupal_http_request();
<?php 

DEFINE(MY_URL, 'http://test.com');

myfunction_init() {

    
  $data = array(
      'my_role' => test,
      'password' => 'test',
      'my_id' => test,
  
    );
    $options = array(
                'method' => 'POST',
                'data' => drupal_http_build_query($data),
                'timeout' => 15,
                'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
    );
    $result = drupal_http_request(MY_URL, $options);

?>
cURL
<?php

function _custommodule_execute($email, $fname, $lname) {

    $id = '94sdf'; 
    $test = 'asdf343'; 

    $url = 'http://test_urlr';
    $query = sprintf('test1=%s&test2=1&test3=%s&test4=%s&test5=%s&test6=%s', $id, $test, $email, $fname, $lname);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    $result = curl_exec($ch);
    $error = curl_error($ch);

    curl_close($ch);

}
?>

Friday, June 17, 2016

add value in header and send in curl

http://www.omaroid.com/php-get-and-set-custom-http-headers/



Sender: 

<?php

$post_url = 'innotech.innoppldesigns.com/useraddapi.php';
$post_string = array(
'names' => 'gnana',
'emails'=> 'gnana@gmail.com',
'password'=> 'gnana',
'role' => 'drupal',
'tech' => 'drupal'
);
$request = curl_init($post_url); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_POSTFIELDS, $post_string); // use HTTP POST to send form data
curl_setopt($request,CURLOPT_HTTPHEADER,array('AUTHORIZATION: innotechhrm'));
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response.
$post_response = curl_exec($request); // execute curl post and store results in $post_response
curl_close ($request); // close curl object 

echo '<pre>';
print_r($post_response);

?>



Receiver : 

if php is installed as an Apache module: 
apache_request_headers()["Authorization"];
else, go to .htaccess file and add:
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
You can then access request headers using any of these:
$_SERVER["HTTP_AUTHORIZATION"]; // using super global