Facebook SDK PHP: send notifications - php

I have an API that serves Android, IOS and Windows Phone apps. The API works fine with Facebook Graph API. I'm trying to create a method that sends a Facebook notification to user, based on this example and Facebook SDK documentation. Here is my code:
public function sendNotification($uid, $msg, $link){
$postdata = "access_token=$this->access_token&href=$link&template=$msg";
$opts = ['http' => ['method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata]];
$context = stream_context_create($opts);
$url = "https://graph.facebook.com/$uid/notifications";
$r = file_get_contents($url, false, $context);
var_dump( $r );
die();
}
My Facebook class has uid, access_token, app_id and app_secret attributes. I should call:
$fb = new Facebook();
$fb->sendNotification($uid, 'Test message', 'http://test.com');
I have the following var_dump:
<b>Warning</b>:
file_get_contents(https://graph.facebook.com/100001795849314/notifications): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in <b>C:\xampp\htdocs\faceteen\api\Classes\Facebook.php</b> on line <b>144</b><br />
bool(false)
I'm running on localhost with Facebook Graph generated token (with manage_notifications perm). I don't want to use Facebook's class, but my own. What am I doing wrong?

Related

How to Add a New Contact in Dynamics 365 using PHP

In a Joomla application I am getting a user info as follows and then I need to save the user info as a contact in a Dynamics 365 database through their REST API.
$user = JFactory::getUser();
$username = $user->username;
$name = $user->name;
I have looked up Dynamics documents around Web API and REST API like this and this, but none of them provide useful info how I can call the API to add a new contact. Currently, I am connecting to Dynamics 365 web application via this url: http://example.com:8088/mysite/api/data/v8.2. The linked post also talks about REST API, but only querying. I'm looking for a way to post data to Dynamics CRM using REST API.
The payload to create Contact using crm webapi will look like this: Read more
POST [Organization URI]/api/data/v8.2/contacts HTTP/1.1
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json
{
"firstname": "Arun",
"lastname": "Vinoth"
}
Sorry am not from PHP background, but this link may help you.
Update:
I browsed little bit. Found the below code sample from SO answer. Update the [Organization URI] with CRM URL, for ex. https://testorg.crm.dynamics.com
$url = '[Organization URI]/api/data/v8.2/contacts';
$data = array('firstname' => 'Arun', 'lastname' => 'Vinoth');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);

php google api contacts 403 in php, working with postman

I want to query the google rest api endpoint to get user contacts:
public static function getContacts(string $token) {
$url = "https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=999999";
$opts = [
"http" => [
"method" => "GET",
"header" => "Authorization: Bearer {$token}"
]
];
$response = file_get_contents($url, false, stream_context_create($opts));
$contacts = json_decode($response);
return $contacts;
}
However, the request returns 403 even thought the token is valid and the request works when sending it via Postman.
Use curl to call api. i think it will work fine there.

Can't get access token from Bigcommerce API, invalid client ID

I am trying to get the access token so I can start building an app that works with BigCommerce. I've been following the docs here: https://developer.bigcommerce.com/api/callback. I'm using the PHP client for Bigcommerce.
The response is HTTP/1.1 400 Bad Request {"error":"Invalid client id."}.
I swear I'm using the correct client id and client secret! Or at least they are what is displayed when I click "View Client ID" on my draft app in the developer portal.
What on earth am I doing wrong?
$request = $_REQUEST;
require_once 'vendor/autoload.php';
use Bigcommerce\Api\Connection;
$tokenUrl = "https://login.bigcommerce.com/oauth2/token";
$connection = new Connection();
$connection->verifyPeer();
$connection->useUrlencoded();
$response = $connection->post($tokenUrl, array(
"client_id" => "", //I won't type it here but it is correct
"client_secret" => "", //also correct
"redirect_uri" => "https://127.0.0.1/project-custom/oauth.php", //this is the Auth Callback URL
"grant_type" => "authorization_code",
"code" => $request["code"], //when I echo these variables out they work
"scope" => $request["scope"],
"context" => $request["context"],
));
print_r($connection->getLastError());
I figured it out!
I just removed the line $connection->useUrlencoded(); because it needed to be sent as "Content-Type: application/json" and I was sending it as "Content-Type: application/x-www-form-urlencoded"

xero api integration in php for a public type application

I want to integrate xero api for public application in php.
I am stuck with oauth application authorization
I have download code from github https://github.com/XeroAPI/XeroOAuth-PHP (find on xero api code sample for public application)
I am using following code:
require('/../lib/XeroOAuth.php');
require('/../_config.php');
$useragent = "Xero-OAuth-PHP Public";
$signatures = array (
'consumer_key' => 'app_consumre_key',
'shared_secret' => 'app_secret_key',
'core_version' => '2.0'
);
$XeroOAuth = new XeroOAuth ( array_merge ( array (
'application_type' => XRO_APP_TYPE,
'oauth_callback' => OAUTH_CALLBACK,
'user_agent' => $useragent
), $signatures ) );
include 'tests.php';
I am passing following xml data:
$xml = "<Invoices>
<Invoice>
<Type>ACCREC</Type>
<Contact>
<Name>Martin Hudson</Name>
</Contact>
<Date>2013-05-13T00:00:00</Date>
<DueDate>2013-05-20T00:00:00</DueDate>
<LineAmountTypes>Exclusive</LineAmountTypes>
<LineItems>
<LineItem>
<Description>Monthly rental for property at 56a Wilkins Avenue</Description>
<Quantity>4.3400</Quantity>
<UnitAmount>395.00</UnitAmount>
<AccountCode>200</AccountCode>
</LineItem>
</LineItems>
</Invoice>
</Invoices>";
$params = array (
'oauth_callback' => OAUTH_CALLBACK
);
$response1 = $XeroOAuth->request ( 'GET', $XeroOAuth->url ( 'RequestToken', '' ), $params );
if ($XeroOAuth->response ['code'] == 200)
{
$outhtoken = $XeroOAuth->response ['response'];
$oauth_exp = explode('&',$outhtoken);
$oauth_exp_token = explode('=',$oauth_exp[1]);
$oauth_token = $oauth_exp_token[1];
}
First I am oauth token, and passing into oauth invoice url
$response = $XeroOAuth->request('POST', $XeroOAuth->url('Invoices', 'core'), array('oauth_token'=>$oauth_token), $xml);
Now I am getting 401 error in response, oauth token mismatch
What mistake I am doing?
If you are getting OAuth errors with Xero, their OAuth Issues article is helpful to provide a possible solution. That said, "token mismatch" isn't mentioned - nor could I find reference to the error in the Xero community.
Based on what you've posted, the first question is if you've completed the OAuth process? There are three main steps (get a request token, user authorisation, get an access token) and your example above only shows the first step. The public.php file you've referenced contains all the steps.
If you do have the OAuth process running smoothly, then make sure the OAuth access token and secret are being passed with your request (only the token is shown in your sample request). You can set these in the XeroOAuth object, so the final request could look like
$XeroOAuth->config ['access_token'] = $oauth_token;
$XeroOAuth->config ['access_token_secret'] = $oauth_token_secret;
$XeroOAuth->request('POST', $XeroOAuth->url('Invoices', 'core'), array(), $xml);
I've made a gist with the complete process based off the XeroOauth-PHP public.php that demonstrates both OAuth and creating an invoice.
Xero is no longer supporting oAuth 1.0
and you must have to use latest Git hub Repo with Xero OAuth 2.0
https://github.com/XeroAPI/xero-php-oauth2

weibo oauth authentication

I used weibo oauth api in magento for connect user with weibo.
But now weibo is broken and it completely get token but display error
when we retrieve user data using authentication token.error is as follows..
i am using this code the use can successfully login but after that there is an error like this
[error_code] => 401
[error] => 40109:consumer_key_refused!
my code is here for after login\
$c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret'] );
$ms = $c->home_timeline();
$me = $c->verify_credentials();
$ms = $c->show_user($userid);
I found weibo new oauth2.0 authentication api that solve my problem.Use this if any one have problem in weibo user authentication..
Weibo-Oauth2 and follow Application scenarios step.
For get access token,you need to use form POST method instead of GET.so you use this code.
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-Type: text/xml\r\n"
)
);
$context = stream_context_create($opts);
$uri= 'https://api.weibo.com/oauth2/access_token?client_id='.WB_AKEY.'&client_secret='.WB_SKEY.'&grant_type=authorization_code&redirect_uri='.YOUR_REGISTERED_REDIRECT_URI.'&code='your authorization code;
$authkey1 = file_get_contents($uri,false,$context);
$decoded_auth1 = json_decode($authkey1,true);
And use this url to get authenticate user data..
$userinfo = file_get_contents("https://api.weibo.com/2/users/show.json?access_token=".$access_token."&uid=".$userid);
$decoded_userinfo = json_decode($userinfo, true);
Hope this help to anyone..
Use weibo_2 with oauth2. please.

Categories