I want to be able for my web site to post a message on the fan page of the website.
So I use this code I found:
<?php
require_once("assets/facebook.php");
$facebook = new Facebook(array(
'appId' => '471898006354908', // Fake
'secret' => 'd2f7fb2dbc0ab7f42bc1c4337ab041b1', // Fake
'cookie' => true
));
$access_token = $facebook->getAccessToken();
echo $access_token;
$msg = "testmsg";
$title = "testt";
$uri = "http://somesite.com";
$desc = "testd";
$pic = "http://static.adzerk.net/Advertisers/d18eea9d28f3490b8dcbfa9e38f8336e.jpg";
$attachment = array(
'access_token' => $access_token,
'message' => $msg,
'name' => $title,
'link' => $uri,
'description' => $desc,
'picture'=>$pic,
'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/me/feed');
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);
?>
Each time I execute the page it echoes me something like this:
471898006354908|d2f7fb2dbc0ab7f42bc1c4337ab041b1
But it don't post anything on my fan page ?
Any help please ?
If that is all the code you found, then you are missing one important part: the login process. It´s explained in detail in the docs: https://developers.facebook.com/docs/facebook-login/v2.4
Right now it seems that you are only using an App Access Token, but you need a User Access Token that is authorized with the publish_actions permission. Make sure you understand all the different Tokens, here are some links about those:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/
Don´t forget to read about Login Review too, if you want to go public with your App: https://developers.facebook.com/docs/facebook-login/review
Using your own CURL calls is perfectly fine btw, i would not suggest using the PHP SDK for small projects because that´s just overkill and the PHP SDK uses CURL too. After all it´s just a bunch of PHP Classes. Just make sure you don´t prefill the message parameter, because that´s not allowed according to the platform policy.
Use the Facebook API / SDK.
This is probably not permitted in ToS and actively prevented. They detect the agent and use other means to prevent you accomplishing this with cURL.
Or you need to at least jump through a couple authentication hoops probably to prevent abuse. Check this past post and please close this if you deem your question to be a dupe. Post to a Facebook user's wall with cURL PHP
Related
The problem
I'm trying to get the e-mail of a user with the Google API.
I want to do it using cURL and PHP.
So basically:
=> You click on a button, you're redirected to https://accounts.google.com/o/oauth2/v2/auth + scope + access_type etc.
=> Once you choosed your account, you're redirected to the new page, and it's in this page that I have the problem.
What I tried
Searching on Stack Overflow and modifying my code but nothing worked. Also, I didn't find anything apart from: This
But there is no solution and it's not really the same problem
The error is
Array
(
[error] => invalid_request
[error_description] => You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy for keeping apps secure. You can let the app developer know that this app doesn't comply with one or more Google validation rules.
)
So I look at the "Google validation rules":
https://developers.google.com/identity/protocols/oauth2/web-server#uri-validation
And, I think that my code respects all theses rules.
Show some code
Note:
$google_id and $google_secret are defined earlier with the correct values.
$code = $_GET["code"];
if (!isset($code)) exit();
# The url oauth2 google API
$url = "https://oauth2.googleapis.com/token";
# The data you want to send via POST
$redirect_uri = urlencode("http://localhost:9000/loginWithGoogleProcess.php"); # redirect_uri
$fields = array(
'code' => $code,
'client_id' => $google_id,
'client_secret' => $google_secret,
'redirect_uri' => $redirect_uri,
'grant_type' => "authorization_code"
);
# Convert array to string URL
$fields_string = http_build_query($fields);
# Open connection
$ch = curl_init();
# Set basic options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
# Curl SSL
# If there is not these lines here, the response will be false
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
# So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# Execute post
$result = json_decode(curl_exec($ch), true);
print_r($result);
And what results from print_r is :
Array
(
[error] => invalid_request
[error_description] => You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy for keeping apps secure. You can let the app developer know that this app doesn't comply with one or more Google validation rules.
)
Dear good people of Stack. I have a custom PHP web site and would like to ask for help in passing new user's name and emails to my GetResponse list. I currently have a Mailchimp integration, which works great, but would like to rewrite it for GetResponse (Please see the code below).
I've consulted this doc, but couldn't do it: https://apidocs.getresponse.com/v3/case-study/adding-contacts
Could someone please help me modify this to work with GetReponse please? I would be very thankful.
\Unirest\Request::auth('Arsen', 'MAILCHIMP_API_KEY');
$body = \Unirest\Request\Body::json([
'email_address' => $_POST['email'],
'merge_fields' => [
'LNAME' => $_POST['name']
],
'status' => 'subscribed'
]);
\Unirest\Request::post(
'MAILCHIMP_LINK' . '/lists/' . 'MAILCHIMP_API_LIST' . '/members',
[],
$body
);
Thank you in advance!
See code below, which works for me. I developed this to gather user's actual IP address (not shown in this example). The API URL is important: should end /contacts ($url below), but this is less than clear in Getresponse documentation. I'm not sure there is a 'status' field in Getresponse, so I have not included that (once added to contacts, the status will be subscribed).
<?php
//API url
$url = 'https://api.getresponse.com/v3/contacts';
// Collection object
// Change YYYYY to campaign ID, called campaign_token in sign-up html forms
$post = ['dayOfCycle' => 0,'email' => $_POST['email'],'name' => $_POST['name'], 'campaign' => ['campaignId' => 'YYYYY']];
// Initializes a new cURL session
$curl = curl_init($url);
// Set the CURLOPT_RETURNTRANSFER option to true
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Set the CURLOPT_POST option to true for POST request
curl_setopt($curl, CURLOPT_POST, true);
// Set the request data as JSON using json_encode function
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post));
// Set custom headers for X-Auth-Token, needed for Getresponse API
// Change ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ to actual API key
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'X-Auth-Token: api-key ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ',
'Content-Type: application/json'
]);
// Execute cURL request with all previous settings
ob_start();
curl_exec($curl);
// close the connection, release resources used
curl_close($curl);
ob_end_clean();
?>
I want to sent documents and photos with the Telegram API. Problem is, that once it's sent, it's cached in on there servers. But if my document changes, I send old versions. For the documentation says:
Pass a file_id as String to send a file that exists on the Telegram
servers (recommended), pass an HTTP URL as a String for Telegram to
get a file from the Internet, or upload a new one using
multipart/form-data
But I'm not sure how to implement this. I tried with the following, sending HTTPHEADER, but it's still sending the cached version :/
function sendPhoto($bot_id,$chat_id,$caption,$disable_notification,$photo_url)
{
$ch = curl_init('https://api.telegram.org/bot'.$bot_id.'/sendPhoto');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$param = array(
'chat_id' => $chat_id,
'caption' => $caption,
'parse_mode' => 'html',
'disable_notification' => $disable_notification,
'photo' => $photo_url
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
Can someone help me please ? :)
As you are sending the file/image by its url, telegram would use its cache and will not fetch the image from that particular given url again (same behaviour may happen when you use your browser to see that image or any resource in internet).
If you want to force telegram to not use the cache, you should change the url by giving some query parameters. In that case, the url would be different than your previous url and telegram will fetch the image again. For example use:
$param = array(
//...
'photo' => $photo_url . '?timestamp=' . time()
);
I'm working to understand how to get the results of a Instagram Subscription that looks for a specific tag. Ultimately, what I would like to do is as images are posted with the tag I'm looking for add the link to the photo as well as the username to a database.
I was able to create my subscription no problem but now I'm not sure how to get the POST information from the subscription.
Working with two files...subscribe.php and callback.php
subscribe.php
<?php
//ALL YOUR IMPORTANT API INFO
$client_id = 'XXX';
$client_secret = 'XXX';
$object = 'tag';
$object_id = 'taglookingfor';
$aspect = 'media';
$verify_token='';
$callback_url = '(full URL here)/callback.php';
//SETTING UP THE CURL SETTINGS...
$attachment = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'object' => $object,
'object_id' => $object_id,
'aspect' => $aspect,
'verify_token' => $verify_token,
'callback_url'=>$callback_url
);
//URL TO THE INSTAGRAM API FUNCTION
$url = "https://api.instagram.com/v1/subscriptions/";
$ch = curl_init();
//EXECUTE THE CURL...
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 THE RESULTS OF THE SUBSCRIPTION, IF ALL GOES WELL YOU'LL SEE A 200
print_r($result);
?>
callback.php
<?php
if (isset ($_GET['hub_challenge'])){
echo $_GET['hub_challenge'];
}
//This is an update
else {
$myString = file_get_contents('php://input');
$answer = json_decode($myString);
echo $answer;
}
?>
In my callback.php I'm attempting to echo out the results of the json_decode...but that also begs the question how will I catch that echo? Sorry, this might be really silly but how do I catch the moment when the callback.php script is being fired by a new image with the specific tag I'm looking for. As I mentioned what I hope to do is take the info in the $answer and insert some of the info into a database.
I'm new at this so any help would be greatly appreciated.
Thanks a million!!
I feel your pain. Their API isn't the easiest to work with and their documentation leaves lot to be desired. That said, this is what happens:
You send a request to the subscription API endpoint
Instagram sends a response to your callback.php file
The response contains a hub_challenge parameter that you need to echo to confirm your subscription
If it's successful you will get a post to your callback.php with the subscriptionid of your new subscription
Everytime you get a post to your callback.php you need to fire a request to Instagram for their latest media that matches your tag.
i think your callback url is not able to receive any post data, you may try to post something to your callback url
I hope this answer of mine will be helpful to people having similar problem.
I would like to upload a photo to facebook for a user in the default album for an application. This is described under publishing here:
http://developers.facebook.com/docs/reference/api/photo
The method has been answered here: How can I upload photos to album using Facebook Graph API. I am using the following:
$args = array(
'message' => 'Photo Caption',
'image' => '#'.realpath("image.png")
);
$data = $facebook->api('/me/photos', 'post', $args);
However I get the exception "(#324) Requires upload file" when I attempt this. I have a valid session and I have the publish_stream and user_photos permissions. I can retrieve data using the API. The image file is definitely valid because it can be loaded with file_get_contents(realpath("image.png")).
I've tried this solution, using curl, which works perfectly:
Upload Photo To Album with Facebook's Graph API
$args = array(
'message' => 'Photo from application',
'pic.png' => '#'.realpath('pic.png')
);
$tok = $session['access_token']
$url = 'http://graph.facebook.com/'.$album_id.'/photos?access_token='.$tok;
$ch = curl_init();
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);
$data = curl_exec($ch);
Compared with Facebook's PHP SDK curl which looks like this (using the same $args and $url):
$ch = curl_init();
$opts = self::$CURL_OPTS;
$opts[CURLOPT_POSTFIELDS] = http_build_query($args, null, '&');
$opts[CURLOPT_URL] = $url;
curl_setopt_array($ch, $opts);
$data= curl_exec($ch);
Why doesn't the PHP version work? Looks like the http_build_query() function is interfering with loading the image. I don't know enough about curl to understand what's going on here.
I'm so glad I went trough the same problem
You have to set the fileUpload param to true !
$facebook = new Facebook(array(
'appId' => $facebookapi_id,
'secret' => $facebookapi_secret,
'fileUpload' => true,
'cookie' => true
));
Facebook have intentionally transformed the POST fields to a GET string using http_build_query() to stop fields beginning with # being used to accidentally or maliciously to upload files. Here's the GitHub issue.
A quick fix for this is to remove the http_build_query() from src/facebook.php in the SDK:
$opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');
Becomes:
$opts[CURLOPT_POSTFIELDS] = $params;
However if you do this you should take action to filter user generated messages that start with #. For example you could add a space to the front of each message.
If you use graph api to upload the photo it will getting the error (#200) User must have accepted TOS.
However if you use old rest api, just changing the url to https://api.facebook.com/method/photos.upload?access_token=xXXXXXXXXXXX if you use above example.