<?php
$appId = 'XXXXX';
$pageId = 'XXXXX';
$secret = 'XXXXX';
$token = 'XXXXX';
$data = array(
'access_token' => $token,
'description' => 'test_description',
'link' => 'http://www.google.co.uk',
'message' => 'test_message',
'name' => 'test_name'
);
try
{
require_once 'facebook/facebook.php';
$sdk = new Facebook(
array(
'appId' => $appId,
'secret' => $secret,
'cookie' => true
)
);
$post = $sdk->api('/'.$pageId.'/feed', 'POST', $data);
print_r($post);
}
catch (FacebookApiException $e)
{
echo $e;
}
?>
Using the code above I am trying to post a link to Google on my page wall. When I run the code, I get an ID response which is what I would expect. However nothing shows up on the page, despite having a valid ID.
If I remove the "link" and try again, I get another ID and the post is visible on my page.
Am I doing something wrong?
Why should the "link" value cause an ID to be returned but no post to be displayed?
This is what Facebook sees (queried via the Graph API explorer):
{
"id": "484729401573953",
"created_time": "2013-01-08T12:03:27+0000",
"caption": "www.readesresidential.com",
"description": "test_description",
"from": {
"name": "David Reade",
"id": "100003544363105"
},
"icon": "http://static.ak.fbcdn.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif",
"link": "http://www.readesresidential.com/brookside-crescent-northop-hall-ch7-6hw-ps03009/",
"message": "test_message",
"name": "test_name",
"picture": "http://external.ak.fbcdn.net/safe_image.php?d=AQC7MpbP6aNe2CVP&w=90&h=90&url=http%3A%2F%2Fwww.readesresidential.com%2Fframework%2Fstatic-487d%2F2%2Fimg%2Fv3%2Ffacebook.png",
"privacy": {
"description": "Friends",
"value": "ALL_FRIENDS",
"allow": "",
"deny": "",
"networks": "",
"friends": ""
}
}
I can see it says "ALL_FRIENDS" - could this be what's causing the post to not be displayed? You can't be friends with a page can you?
I have now modified the "data" variable to the following:
$data = array(
'access_token' => $token,
'description' => 'test_description',
'link' => 'http://www.readesresidential.com/go/ps02618',
'message' => 'test_message',
'name' => 'test_name',
'privacy' => array(
'value' => 'EVERYONE'
)
);
The response from the Graph API explorer is:
{
"id": "454654274601000",
"created_time": "2013-01-08T12:14:52+0000",
"caption": "www.readesresidential.com",
"description": "test_description",
"from": {
"name": "David Reade",
"id": "100003544363105"
},
"icon": "http://static.ak.fbcdn.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif",
"link": "http://www.readesresidential.com/church-street-tarvin-ch3-8eb-ps02618/",
"message": "test_message",
"name": "test_name",
"picture": "http://external.ak.fbcdn.net/safe_image.php?d=AQC7MpbP6aNe2CVP&w=90&h=90&url=http%3A%2F%2Fwww.readesresidential.com%2Fframework%2Fstatic-487d%2F2%2Fimg%2Fv3%2Ffacebook.png",
"privacy": {
"description": "Public",
"value": "EVERYONE",
"allow": "",
"deny": "",
"networks": "",
"friends": ""
}
}
Even though the post is now "public" and I have used a completely new URL, it's still not showing.
Could this be a Facebook bug?
Why should the "link" value cause an ID to be returned but no post to be displayed?
This usually happens if the same link is posted again by the same user (with only a certain amount of time between those two posts).
After some fiddling with the code, I found out how to fix this. Turns out the access token I was using was incorrect, even though the debugger tool said it was valid and had the appropriate permissions.
I had to query the Graph API, in particular the /me/accounts bit using the access token which returned the page info but and a completely different access token. This access token in question has no expiration time (I thought this feature was no more??) and after inserting it into my code, the posts now display properly for all users.
None of these steps were in the Facebook documentation anywhere, and if they are then they're extremely hard to find.
Related
I am using the MPESA-Express also called the STK push api V1 to receive payments from my clients.
To get the customer paying, I am looking for the PhoneNumber value in the results body of the response if the payment is successful. This way I can associate a payment with a customer.
However now that we'll be having data minimisation on the MPesa api, the PhoneNumber will not be displayed fully, and I am facing a challenge of how to associate a payment transaction with a client. I have tried setting the AcccountReference in the request as shown below, but I can't get this AccountReference back in the response results body. I was thinking of setting a unique AcccountReference for each customer.
The data I am sending to the endpoint https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest
$postData = json_encode([
"BusinessShortCode" => Yii::$app->params['businessShortCode'],
"Password" => $this->createMpesaRequestsPassword($timestamp),
"Timestamp" => $timestamp,
"TransactionType" => $transactionType,
"Amount" => $amount,
"PartyA" => $phoneNumber,
"PartyB" => Yii::$app->params['businessShortCode'],
"PhoneNumber" => $phoneNumber,
"CallBackURL" => $callBackUrl,
"AccountReference" => $phoneNumber,
"TransactionDesc" => $transactionDesc
]);
On my callback url I get this response:
{
"Body": {
"stkCallback": {
"MerchantRequestID": "9183-42212949-1",
"CheckoutRequestID": "ws_CO_23072022133552132714385056",
"ResultCode": 0,
"ResultDesc": "The service request is processed successfully.",
"CallbackMetadata": {
"Item": [
{
"Name": "Amount",
"Value": 1
},
{
"Name": "MpesaReceiptNumber",
"Value": "QGN2XSH6MQ"
},
{
"Name": "Balance"
},
{
"Name": "TransactionDate",
"Value": 20220723133617
},
{
"Name": "PhoneNumber",
"Value": 254711111111
}
]
}
}
}
}
How do know which transaction belongs to which user?
As from Facebook documentation I can get the count of shares for a web page calling the following url https://graph.facebook.com/?id=http://www.google.it, the returned data are the following:
{
"share": {
"comment_count": 0,
"share_count": 636734
},
"og_object": {
"id": "389545309239",
"title": "Google",
"type": "website",
"updated_time": "2017-06-08T10:05:50+0000"
},
"id": "http://www.google.it"
}
I want to get the same data using the PHP SDK using the following code:
//require the Facebook PHP SDK
$client_id="my app id";
$client_secret="my secret key";
$default_access_token = file_get_contents( "https://graph.facebook.com/oauth/access_token?client_id=$client_id&client_secret=$client_secret&grant_type=client_credentials");
$OGResponse = new Facebook\Authentication\AccessToken($default_access_token);
$access_token = json_decode($OGResponse)->access_token;
$fb = new Facebook\Facebook([
'app_id' => $client_id, // Replace {app-id} with your app id
'app_secret' => $client_secret,
'default_access_token' => $access_token,
]);
$helper = $fb->getRedirectLoginHelper();
$request = new \Facebook\FacebookRequest(
$fb->getApp(),
$access_token,
'GET',
'/',
array(
'id' => 'http://www.google.it',
)
);
$response = $fb->getClient()->sendRequest($request);
//$response = $request->execute();
$graphObject = $response->getGraphObject();
Accessing the $graphObject I can retrieve the same data except the ones in "share" key, that are what I'm interested.
If I use the GRAPH API Explorer, I get the same info using the PHP SDK:
{
"og_object": {
"id": "389545309239",
"title": "Google",
"type": "website",
"updated_time": "2017-06-08T10:05:50+0000"
},
"id": "http://www.google.it"
}
Is there any way to get the "shares" using the PHP SDK?
try this
graph api explorer v2.8
but you cannot get share field with graph api v2.9 because share field is deprecated for versions v2.9 and higher,or you can use engagement field like this graph api explorer v2.9
for PHP SDK:
$request = new \Facebook\FacebookRequest(
$fb->getApp(),
$access_token,
'GET',
'/',
array(
'id' => 'http://www.google.it',
'fields' => 'engagement,og_object',
)
);
then you will get something like this
{
"engagement": {
"reaction_count": 207037,
"comment_count": 125335,
"share_count": 304362,
"comment_plugin_count": 0
},
"og_object": {
"id": "389545309239",
"title": "Google",
"type": "website",
"updated_time": "2017-06-08T15:58:56+0000"
},
"id": "http://www.google.it"
}
I'm having an issue with Facebook's PHP SDK version 4.0.
I'm simply trying to retrieve the insights/page_fans_city metric via the Graph API:
$request = new FacebookRequest($session, 'GET',
'/{my object}/insights/page_fans_city',
array('period' => 'lifetime'));
$response = $request->execute();
$client_graph_object = $response->getGraphObject();
print_r($client_graph_object);
However, no data is returned:
Facebook\GraphObject Object
(
[backingData:protected] => Array
(
[data] => Array
(
)
[paging] => stdClass Object
(
[previous] => https://graph.facebook.com/v2.0/...
[next] => https://graph.facebook.com/v2.0/...
)
)
)
I know that I have the Insights data that I'm requesting. I can make the exact same request via the Graph Explorer:
→ /v.2.0/{my object}/insights/page_fans_city?period=lifetime
{
"data": [
{
"id": "{my object}/insights/page_fans_city/lifetime",
"name": "page_fans_city",
"period": "lifetime",
"values": [
{
"value": {
...
},
"end_time": "2014-08-01T07:00:00+0000"
},
{
"value": {
...
},
"end_time": "2014-08-02T07:00:00+0000"
}
],
"title": "Lifetime Likes by City",
"description": "Lifetime: Aggregated Facebook location data, sorted by city, about the people who like your Page. (Unique Users)"
}
],
"paging": {
"previous": "https://graph.facebook.com/v2.0/...,
"next": "https://graph.facebook.com/v2.0/...
}
}
I should note that I am receiving data if I omit the page_fans_city segment. When it's omitted- I can view data for page_fans_country only. Therefore
$request = new FacebookRequest($session, 'GET',
'/{myobject}/insights/page_fans_country',
array('period' => 'lifetime'));
and
$request = new FacebookRequest($session, 'GET',
'/{myobject}/insights',
array('period' => 'lifetime'));
will work for me ...
I suspect it's my Access token- but I'm not sure why that would be the case. The app that I created only requires the read_insights permission.
Any thoughts or suggestions?
Thanks in advance,Mike
It was a permissions issue. If you ever see:
{
"data": [
],
...
}
It's likely your access token does not have sufficient privileges to access the data you're requesting (that or there's simply no data).
My problem was that even though I was the Developer of the app and it was approved by Facebook (with the manage_pages and read_insights permissions)- I could not use an app access token to retrieve the insights data.
I needed a user access token!
I easily generated the user access token by manually creating a login flow per Facebook's documentation. This stackoverflow thread was useful, too.
More info on Facebook tokens here.
Hope this helps anyone who stumbles across this thread!
-Mike
I am having trouble getting any data out of a mashape API, I have made the UNIREST POST but I am unable to echo the data back to myself, this is what the call should return;
{
"result": {
"name": "The Elder Scrolls V: Skyrim",
"score": "92",
"rlsdate": "2011-11-11",
"genre": "Role-Playing",
"rating": "M",
"platform": "PlayStation 3",
"publisher": "Bethesda Softworks",
"developer": "Bethesda Game Studios",
"url": "http://www.metacritic.com/game/playstation-3/the-elder-scrolls-v-skyrim"
}
}
And my code...
require_once 'MYDIR/mashape/unirest-php/lib/Unirest.php';
$response = Unirest::post(
"https://byroredux-metacritic.p.mashape.com/find/game",
array(
"X-Mashape-Authorization" => "MY API AUTH CODE"
),
array(
"title" => "The Elder Scrolls V: Skyrim",
"platform" => undefined
)
);
echo "$response->body->name";
?>
Can anyone suggest how I can get it to echo the "name".
Any help would be appreciated :)
It appears the way you are trying to display the body is preventing you from seeing an error in the response telling you that the property platform is required to be a number.
Try using json_encode($response->body) instead to see the full response:
<?php
require_once 'lib/Unirest.php';
$response = Unirest::post(
"https://byroredux-metacritic.p.mashape.com/find/game",
array(
"X-Mashape-Authorization" => "-- your authorization key goes here --"
),
array(
"title" => "The Elder Scrolls V: Skyrim",
"platform" => 1 # try placing undefined here.
)
);
echo json_encode($response->body);
?>
Once you've gotten the response you can then use $response->body->result->name:
$result = $response->body->result;
echo "{$result->name} has a score of [{$result->score}]";
I have a Mongo document as presented below. Using PHP I need to update the password of an account, I could access it by either email or id.
I thought the following code will bring focus to the part of the document when executing an update:
$criteria = array("accounts.email" => $emailAddress);
$document = array(
'$set' => array(
"accounts.password" => $newPassword
)
);
But when doing an update nothing gets updated. Any idea how I could update the password by only providing the email address or id?
{
"accounts": [
{
"id": ObjectId("5266cf3c9b7e7a292b000002"),
"main": true,
"username": "username_a",
"password": "password_a",
"email": "a#a.com"
},
{
"id": ObjectId("5266cf3c9b7e7a292b000004"),
"main": true,
"username": "username_a",
"password": "password_b",
"email": "b#b.com"
}
],
"active": true
}
Use the positional $ operator.
$criteria = array("accounts.email" => $emailAddress);
$document = array(
'$set' => array(
'accounts.$.password' => $newPassword
)
);
$collection->update($criteria, $document);