How to share a public photo in a group php sdk facebook - php

I'm trying to share a public photo to a group using php sdk (graph api)
$ret_obj = $facebook->api("/$group_id/feed", 'POST', array(
'link' => $link,
'message' => $message,
)
);
where $link is a link for the photo I want to share in this format : https://www.facebook.com/photo.php?fbid=10151273906386749&set=a.53081056748.66806.6815841748&type=1&theater
it didn't work so I also tried this in Graph Api Explorer,
in result i get :
{
"error": {
"message": "(#100) property values must be strings or objects with a 'text' and a 'href' attribute",
"type": "OAuthException",
"code": 100
}
}

Related

How to create course work for a Google Classroom project using API in php

I want to create a course work(Assignment) as a course teacher using Google Classroom API to itself for a project in PHP. To create an assignment which correct code I wrote? Give me some suggestions with PHP code for creating an assignment.
I've added some code to the quickstart.php file.
Code:
$client = getClient();
$courseId = '394192735087';
$service = new Google_Service_Classroom($client);
$post_body = new Google_Service_Classroom_CourseWork(array(
'workType' => 'ASSIGNMENT',
'title' => 'Quiz-5',
'description' => 'where you add up by the number of numbers',
'state' => 'PUBLISHED',
'maxPoints' => 100,
'associatedWithDeveloper' => true,
'assigneeMode' => 'ALL_STUDENTS',
'submissionModificationMode' => 'SUBMISSION_MODIFICATION_MODE_UNSPECIFIED'
));
$service->courses_courseWork->create($courseId, $post_body);
But when I run this code in quickstart.php at localhost the following problems can be seen.
Fatal error: Uncaught Google\Service\Exception: {
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"name\" at 'course_
work': Cannot find field.",
"errors": [
{
"message": "Invalid JSON payload received. Unknown name \"name\" at 'cou
rse_work': Cannot find field.",
"reason": "invalid"
}
],
"status": "INVALID_ARGUMENT"
}
}
in C:\xampp\htdocs\api\vendor\google\apiclient\src\Http\REST.php:128
How can I solved it ?

Validate GET status of a url for mailchimp

I have created a function that creates a batch webhook in mailchimp so that a callback will be submitted once the batch operations is finished. like following:
// register mailchimp batch webhook
$responseWH = wp_remote_post( 'https://' . substr($mcApi,strpos($mcApi,'-')+1) . '.api.mailchimp.com/3.0/batch-webhooks' ,array(
'headers' => array(
'method' => 'POST',
'Authorization' => 'Basic ' . base64_encode( 'user:'. $mcApi )
),
'body' => json_encode(array(
'url' => 'http://90660d72b8be.ngrok.io/wp-json/lubuvna/v2/batch/2810471250791421617231098394447326550803'
))
));
above code means, when a batch operation is finished MC should call this url:
http://90660d72b8be.ngrok.io/wp-json/lubuvna/v2/batch/2810471250791421617231098394447326550803
If this url is called, then a script will run and notify me that the operation is done und update the the post in wordpress.
but since the header status code is 404, mailchimp is not adding the url to the batch webhook.. instead i get following error:
{
"type": "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
"title": "Invalid Resource",
"status": 400,
"detail": "The resource submitted could not be validated. For field-specific details, see the 'errors' array.",
"instance": "05a4430f-c68f-46a5-82af-3b95332d6fe83",
"errors": [
{
"field": "url",
"message": "We couldn't verify the URL is working. Please double check and try again. HTTP Code: 500"
}
]
}
How can i return a 200 status in the header when Mailchimp is trying to make a GET request to the specified URL, while adding the batch webhook? they need a valid url. mailchimp batch webhook
I am using ngrok for connection within localhost which works when i add a URL like http://90660d72b8be.ngrok.io/wp-content/plugins/my-plugin/inc/batch/import.php, because the file actually exist in the plugin directory
but when adding the URL http://90660d72b8be.ngrok.io/wp-json/lubuvna/v2/batch/2810471250791421617231098394447326550803
I can see that mailchimp makes a get request and the code status is 404 like in the screenshot
I hav ended up using follwoing:
function myplugin_registered_rest_routes() {
// batch operation finished
register_rest_route('myplugin/v2', '/batch/(?P<id>\d+)',
array(
array('methods' => ['POST', 'GET'],
'callback' => 'my_awesome_func'
)
)
);
}
add_action( 'rest_api_init', 'myplugin_registered_rest_routes' );
another option of array like #04FS answer:
register_rest_route('myplugin/v2', '/batch/(?P<id>\d+)',
array(
array('methods' => 'GET',
'callback' => 'my_awesome_func',
),
array('methods' => 'POST',
'callback' => 'my_awesome_func'
)
)
);
Another option parsing the url request. Not common when using wordpress API:
add_action('parse_request', 'register_endpoint', 0);
function register_endpoint() {
global $wp;
if ($wp->request == 'wp-json/myplugin/v2/batch/2810471250791421617231098394447326550803') {
header("HTTP/1.1 200 OK");
exit;
}
}

facebook open graph count shares using PHP SDK

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"
}

Posting on google profile to any user in php

I want to posting on google plus account to any user who authenticate my app i have already create G-Suite account and i can also post on google plus account but i can not post on other user account who already done app authentication i want the functionality like a www.buffer.com application they can easily post on all the users who have verified the google profile so i want to know how they can post on google account and i can't upload activity on google+ accopunt.
$service = new Google_Service_PlusDomains($client);
$activity = new Google_Service_PlusDomains_Activity(
array(
'access' => array(
'items' => array(
'type' => 'domain'
),
'domainRestricted' => true
),
'verb' => 'post',
'object' => array(
'originalContent' => "Post using Google API PHP Client Library! 1" ,
'attachments' => ['image'=>array('url'=>$img_path)]
),
)
);
$newActivity = $service->activities->insert($user->id, $activity);
var_dump($newActivity);
It will successfully post on my google plus account means my G-suit account login but whenever i am going to try to send post to on other account then it is not possible to send post on other normal account it will throwing me error like
ERROR :
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "Forbidden"
}
],
"code": 403,
"message": "Forbidden"
}
The Domains API can only be used by G Suite users. It doesn't matter who creates the app, it matters which specific account credentials are on each specific request.

Posting a link to a page not showing up?

<?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.

Categories