Get to know projecthelper

projecthelper.php help you make the call to api via curl and contains other utilities

<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

class projecthelper {



/**
* return string like http://yourdomain.com/call_back_file.
* @param type $call_back_file
* @return string
*/
public static function get_base_url($call_back_file) {
if (isset($_SERVER['HTTPS'])) {
return 'https://' . $_SERVER['SERVER_NAME'] . '/' . $call_back_file;
} else {
return 'http://' . $_SERVER['SERVER_NAME'] . '/' . $call_back_file;
}
}





public static function _mapper_array($input, $extend = null) {
$ret = array();
foreach ($input as $value) {
$ret[$value['name']] = $value['values'][0];
}
$ret['extend'] = $extend;
return $ret;
}

public static function make_request($endpoint, $header = null, $post_fields = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

if ($header != null) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
if ($post_fields != null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}
$data = curl_exec($ch);

if (curl_errno($ch) > 0) {
return (curl_error($ch));
}

curl_close($ch);

return self::convert_string_to_array($data);
}

public static function get_tutorial_array() {
return array(
'facebook-login',
'google-login',
'twitter-login',
'tumblr-login',
'instagram-login',
'vk-login',
'yandex-login',
'reddit-login',
'vimeo-login',
'getresponse-login',
'flickr-login',
'esty-login',
'dailymotion-login',
'meetup-login',
'linkedin-login',
'hotmail-login',
'box-login',
'dropbox-login',
'imgur-login',
'mailchimp-login',
'fitbit-login',
'discogs-login',
'amazon-login',
'paypal-login',
'foursquare-login',
'bitbucket-login',
'disqus-login',
'buffer-login',
'bitly-login',
'evernote-login'
);
}

public static function convert_string_to_array($input) {
$ret = json_decode($input, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$pos = strpos($input, '&');
if ($ret != null) {
return (array) $ret;
} else if ($input != strip_tags($input)) {
// contains HTML tags
return $input;
} else if ($pos != false) {
$ret = array();
$value_array = explode('&', $input);
if (count($value_array)) {
foreach ($value_array as $data) {
$key_data = explode('=', $data);
if (count($key_data) > 0) {
$ret[$key_data[0]] = $key_data[1];
}
}
}
return $ret;
} else {
return $input;
}
}

}

Here is some important functions:


make_request($endpoint, $header = null, $post_fields = null)

Send request to api, return arrray, if there is error print on the screen

Example

  • $config = projecthelper::get_google_config();
    $code = $_GET['code'];
    $endpoint = 'https://accounts.google.com/o/oauth2/token';
    $post_fields = 'code=' . $code .
    '&grant_type=authorization_code' .
    '&client_id=' . $config['client_id'] .
    '&client_secret=' . $config['client_secret'] .
    '&redirect_uri=' . $config['redirect_uri'];
    $header = array(
    'POST /o/oauth2/token HTTP/1.1',
    'Host: accounts.google.com',
    );

    $data = projecthelper::make_request($endpoint, $header, $post_fields);

convert_string_to_array($input)

convert string to array

Example:

  • $data = 'oauth_token=fzMrQjsje0ecgPYj7DZ6uGwzMs3koY4mCbfwGqJNYeiR7DtCWG&oauth_token_secret=cOdIqDF4oJesKsbquBXGUzH9vHQA9MhXyD3h3N4vproo4S0Vn7&oauth_callback_confirmed=true';
    $ret = projecthelper::convert_string_to_array($data);
  • The result is
    array (size=3)'oauth_token' => string 'fzMrQjsje0ecgPYj7DZ6uGwzMs3koY4mCbfwGqJNYeiR7DtCWG' (length=50)'oauth_token_secret' => string 'cOdIqDF4oJesKsbquBXGUzH9vHQA9MhXyD3h3N4vproo4S0Vn7' (length=50)'oauth_callback_confirmed' => string 'true' (length=4) 

get_base_url($call_back_file)

return uri like http://yourdomain/$call_back_file

Example:

  • $url = projecthelper::get_base_url('google-login.php');

If you have any question, please tell us, or read google login tutorial to get more about usage