Are facebook notifications from either vb.net or PHP? - php

Does anyone know if its possible to create a Facebook notification either via Vb.Net or PHP using the Facebook SDK version 4.
Ideally I'd like to have users on my website enter their Facebook login details which I'd then store their [facebook id] so I could send a weekly status notification via VB.Net to their Facebook account.
Edited
So far I get the scope ID from javascript and for testing copy it to PHP
<?php
$appId = '77585851581xxxx';
$appSecret = 'xxxx';
$userId = '1015273548332xxxx'; //Scope ID
$accesstoken = $appId . '|' . $appSecret;
$notificationText = 'The cake is a lie.';
//Get Access Token
$args = array('grant_type' => 'client_credentials',
'client_id' => $appId,
'client_secret' => $appSecret);
$ch = curl_init();
$url = 'https://graph.facebook.com/oauth/access_token';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$returnValue = curl_exec($ch);
curl_close($ch);
echo $returnValue;
$accesstoken = $returnValue;
//Set Notification
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook');
$url = 'https://graph.facebook.com/' . $userId . '/notifications' .
'?access_token=' . $accesstoken .
'&template=' . $notificationText .
'&href=';
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);
echo $curlResult;
?>
I now get the error:
Sorry, something went wrong. We're working on getting this fixed as soon as we can.

Tutorial for the PHP SDK 4.0: http://www.devils-heaven.com/facebook-php-sdk-4-0-tutorial/
Although i would suggest using simple CURL calls for App Notifications, you donĀ“t need the PHP SDK for that. Example code can be found here: http://blog.limesoda.com/2014/08/app-notifications-facebook-apps/
The article is in german, but the code is the relevant part:
$accessToken = $appId . '|' . $appSecret;
$notificationText = 'The cake is a lie.';
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook');
//send notification
$url = 'https://graph.facebook.com/' . $userId . '/notifications' .
'?access_token=' . $accesstoken .
'&template=' . $notificationText .
'&href=';
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);

Related

reddit api 403 forbidden

I have the following code;
It all works and I am able to obtain an access token but I get a 403 Forbidden Error when trying to use the submit endpoint. I saw a previous post that uses identical code pretty much, that mentioned that the endpoint is incorrect, Original Post.
I can't seem to find the updated endpoint; Is anyone familiar with the reddit api and would be able to help find the problem here. Thanks :-)
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$clientID = '****'; // app client id
$clientSecret = '****'; // app secret
$redditUsername = '****'; // reddit username
$redditPassword = '****'; // reddit password
// Post to subreddit info
$title = '****';
$r = '****';
$kind = '****';
$url = 'http://example.com';
$ch = curl_init('https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&username=' . $redditUsername . '&password=' . $redditPassword . '');
curl_setopt($ch, CURLOPT_USERPWD, '' . $clientID . ':' . $clientSecret . '');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
$token = json_decode($return, true);
print_r($token);
$ch = curl_init('https://www.reddit.com/api/v1/submit?kind=link&sr=' . $r . '&title=' . $title . '&r=' . $r . '&url='.urlencode($url));
curl_setopt($ch, CURLOPT_USERPWD, '' . $clientID . ':' . $clientSecret . '');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: bearer " . $token['access_token'] . ""));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
$response = json_decode($return, true);
echo '<br><br>';
print_r($response);
?>

Binance REST API - Placing a PHP Order (POST) via Query String

I am struggling using Binance's REST API. I have managed to get working GET request via query string such as pinging the server, ticker information, etc. My challenge now is performing POST request via query string using cURL. I have been scraping code from various places and referring back to the API to get pieces to work but I am unsure as to why I am getting this error returned from the result... {"code":-1102,"msg":"Mandatory parameter 'signature' was not sent, was empty/null, or malformed."}
(ERROR SHOWN ON WEBPAGE). I echo out the signature and its a load of gibberish so I would believe that the hash_hmac performed at the top would be working, but honestly I got pretty lucky making the GET request work. Does anyone have any suggestions as to why this would be broken? Thanks!
$apikey = "MYKEY";
$apisecret = "MYSECRET";
$timestamp = time()*1000; //get current timestamp in milliseconds
$signature = hash_hmac('sha256', "TRXBTC&type=market&side=buy&quantity=100.00&recvWindow=10000000000000000&timestamp=".$timestamp, $apisecret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.binance.com/api/v3/order/test");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "symbol=TRXBTC&type=market&side=buy&quantity=100.00&recvWindow=10000000000000000&timestamp=".$timestamp);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$apikey,"signature: ".$signature));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
As per their API docs:
SIGNED endpoints require an additional parameter, signature, to be sent in the query string or request body.
You are sending the signature via neither of these methods and are instead sending it through the header.
Change this:
curl_setopt($ch, CURLOPT_POSTFIELDS, "symbol=TRXBTC&type=market&side=buy&quantity=100.00&recvWindow=10000000000000000&timestamp=".$timestamp);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$apikey,"signature: ".$signature));
To this:
curl_setopt($ch, CURLOPT_POSTFIELDS, "symbol=TRXBTC&type=market&side=buy&quantity=100.00&recvWindow=10000000000000000&timestamp=" . $timestamp . "&signature=" . $signature);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$apikey));
<?php
$secret = "F................";
$key = "D.................";
$s_time = "timestamp=".time()*1000;
$sign=hash_hmac('SHA256', $s_time, $secret);
$url = "https://api.binance.com/api/v3/account?".$s_time.'&signature='.$sign;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-MBX-APIKEY:'.$key));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
$result = json_decode($result, true);
echo '<pre>';
var_dump($result);
echo '</pre>';
?>
Here is an example, using php-curl-class
// Variables
// url, key and secret is on separate file, called using require once
$endPoint = "/api/v3/order/test";
$coin = "BTC";
$fiat = "EUR";
$symbol = $coin . "" . $fiat;
$side = "BUY";
$type = "LIMIT";
$timeInForce = "GTC";
$quantity = 1;
$price = 10000;
$timestamp = time();
// Constructing query arrays
queryArray = array(
"symbol" => $symbol,
"side" => $side,
"type" => $type,
"timeInForce" => $timeInForce,
"quantity" => $quantity,
"price" => $price,
"timestamp" => $timestamp*1000
);
$signature = hash_hmac("sha256", http_build_query($queryArray), $secret);
$signatureArray = array("signature" => $signature);
$curlArray = $queryArray + $signatureArray;
// Curl : setting header and POST
$curl->setHeader("Content-Type","application/x-www-form-urlencoded");
$curl->setHeader("X-MBX-APIKEY",$key);
$curl->post($url . "" . $endPoint, $curlArray);
if ($curl->error) {
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
}
$order = $curl->response;
print_r($order);
I had the same problem, and nothing of above doesn't helped.
So I finaly figured out how to make order on my way.
So, maybe this helps someone.
function Kupovina($buy_parametri) {
$key = "xxxxxxxxxxxxxxx";
$secret = "xxxxxxxxxxxx";
$s_time = "timestamp=".time()*1000;
$timestamp = time()*1000; //get current timestamp in milliseconds
$sign = hash_hmac('sha256', $buy_parametri."&timestamp=".$timestamp, $secret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.binance.com/api/v3/order");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $buy_parametri."&".$s_time."&signature=".$sign);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$key));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$buy_parametri = "symbol=BTCUSDT&type=market&side=buy&quantity=0.00086";
Call function:
Kupovina($buy_parametri);

Publish on Facebook page with PHP

I was using CURL to publish on Facebook page but now it's not working anymore.
I need to publish on Facebook page using GraghAPI and CURL from my website to my pages.
This was code used before it was working for about 4 months ago but now not working:
$page_access_token = 'PAGE_ACCESS_TOKEN';
$page_id = 'PAGE_ID';
$data['picture'] = $news_image;
$data['link'] = $url_link;
$data['message'] = $title;
$data['caption'] = $title;
$data['description'] = $disc;
$data['access_token'] = $page_access_token;
$post_url = 'https://graph.facebook.com/' . $page_id . '/feed';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
How to publish to Facebook page without using Facebook App as it need a lot of verification to make an app.

Shutterstock - lightboxes - Missing required scope error

I am trying to get my lightbox-collection of images from shutterstock. I am using php (wordpress site). For that I followed examples in documentation, but applied the code for my app.
To get token I use this code:
$client_id = 'My CLIENT ID here.';
$client_secret = 'My client secret there';
$redirect_uri = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . "{$_SERVER['HTTP_HOST']}" . strtok($_SERVER['REQUEST_URI'], '?');
if(isset($_GET['code'])) {
$url = 'https://accounts.shutterstock.com/oauth/access_token';
$params = array(
code => $_GET['code'],
scope => 'collections.view',
client_id => $client_id,
client_secret => $client_secret,
redirect_uri => $redirect_uri,
grant_type => 'authorization_code'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response);
if (json_last_error()) {
echo '<span style="font-weight:bold;color:red;">Error: ' . $response . '</span>';
} else {
$_SESSION['access_token'] = $json->access_token;
}
After that to get lightbox I go with this code:
...
$url = 'https://api.shutterstock.com/v2/images/collections/33162025/items';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $this->accessToken));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response);
However, instead of request with lightbox info, I get this error:
Missing required scope: collections.view
As you see I have scope parameter in my parameters array. What am I missing there?
You are passing the scope after you have got the authorization which isn't right.
Pass the scope when you request for access token, see the case underneath.
https://accounts.shutterstock.com/oauth/authorize?client_id=xxxx&client_secret=yyyy&redirect_uri=http://zzz/zbc.php&scope=collections.view
Just replace your credentials and copy the url to the browser and it will definitely work.
Cheers :)

How to post on user's wall in facebook after getting permission from user?

Is there any good tutorial that teaches how to post on user's wall with API? This is the flow:
The user will come on my website and click on post to facebook button at the end of article.
He is shown sign in dialog from facebook, after sign in he will give permission to my application to post on his wall on his behalf.
After authorization, his shared link will be posted on his wall.
Upon future shares, he will not be asked for permissions since he has already given permission to post on his behalf. So in future when he clicks 'Post to Facebook' button under the article then that item will be posted to his wall without opening facebook login dialog.
I have searched a lot on tutorials but have not found any that meets my requirement.
I am very new to facebook API and have not worked with it before.
Any suggestions or link to tutorials?
Thank You!
I've code to help you to post status with an image to an user's timeline.
After user has given permission to your app , you might have received a query string parameter called 'code' , using this code , we can post to user's timeline.
$code = #$_REQUEST["code"];
if(!empty($code))
{
// Start : Get live user access token ------------------------------------------ //
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . FACEBOOK_APP_ID
. "&redirect_uri=" . urlencode( FACEBOOK_POST_LOGIN_URL)
. "&client_secret=" . FACEBOOK_APP_SECRECT
. "&code=" . $code;
$token = $this->get_app_access_token(FACEBOOK_APP_ID,FACEBOOK_APP_SECRECT,$token_url);
$access_token = $token;
// End : Get live user access token ------------------------------------------- //
// Start : Create album ------------------------------------------------------ //
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$uri = 'https://graph.facebook.com/albums?access_token='.$access_token;
$post_fields = array('name' => trim( FACEBOOK_ALBUM_NAME ));
$curl = curl_init( $uri );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_POST, TRUE );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $post_fields );
$raw_data = curl_exec($curl);
curl_close( $curl );
$created_album_id = json_decode( $raw_data, $assoc = TRUE );
// End : Create album ---------------------------------------------------------- //
$facebook_share_image_url = FACEBOOK_SHARE_IMAGE_PATH;
$facebook_status_text = 'The status text';
$graph_url= "https://graph.facebook.com/me/photos";
$postData = "url=" . urlencode($facebook_share_image_url)
. "&message=" . urlencode($facebook_status_text)
. "&access_token=" .$access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
curl_close($ch);
// End : Add photos ------------------------------------------------------------- //
}
and the function to get app access token
function get_app_access_token($app_id, $secret,$token_url)
{
$url = 'https://graph.facebook.com/oauth/access_token';
$token_params = array(
"type" => "client_cred",
"client_id" => FACEBOOK_APP_ID,
"redirect_uri" => urlencode(FACEBOOK_POST_LOGIN_URL),
"client_secret" => FACEBOOK_APP_SECRECT
);
$a1 = $this->file_get_contents_curl($token_params,$token_url);
$a2 = str_replace("access_token=","",$a1);
$a3 = explode("&expires",$a2);
return $a3[0];
}
The other function access graph url
function file_get_contents_curl($params,$url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
$headers = array(
"Cache-Control: no-cache",
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
Hope it helps..!!
You can use simple facebook sharer URL:
https://www.facebook.com/sharer/sharer.php?u=YOURPAGEURL
Where YOURPAGEURL is the page URL which you want to share.. Hope this helps.
you have to permission pubish_stream on your app and then try this using curl:-
Also you need a user access token then send access token with curl call
$attachment = array(
'access_token' => $access_token,
'message' => 'i m success to using graph api for post wall',
'name' => 'Wall Post using graph api',
'link' => 'www.mysite.com',
'description' => 'Using the Graph API, any Facebook users Wall feed may be accessed by using this URL:',
'picture'=>'http://example.com/images/noimage.png'
);
$url = "https://graph.facebook.com/$facebook_id/feed";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
print_r($result)
For more :- Graph API new feed post object-Attachment not showing
Post to a Facebook user's wall with cURL PHP

Categories