The usual way as I post messages to my page wall is like:
$args = array(
'access_token' => $page_access_token,
'message' => $title,
'link' => $link,
'name' => 'This is title',
'description' => 'This is a testing of the posting on page',
//'picture' => 'http://www.example.com/directory/images/30.jpg'
);
$post_id = $facebook->api("/$pageId/feed","post",$args);
But how can I post an images to my wall - the alternative to: click to UPLOAD button, pick out an image -> and upload, image is on to wall.
I have on my FTP some images and these ones I would like to upload to my wall.
Thanks in advance
https://developers.facebook.com/blog/post/498/ this link can help you...
Here are some various ways to upload photos using the Graph API. The examples assume you've instantiated the $facebook object and have a valid session.
1 - Default Application Album of Current User
This example will upload the photo to your default application album of the current user. If the album does not yet exist it will be created.
$facebook->setFileUploadSupport(true);
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $args);
print_r($data);
2 - Target Album`
This example will upload the photo to a specific album.
$facebook->setFileUploadSupport(true);
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);
I'm NOT using the Facebook API include, so i'm trying to integrate Facebook stuff using only curl requests directly to the Graph API URL.
So, to post images in the User's wall, the simplest way I found is just to indicate the Image URL.
<?php
$url = "https://graph.facebook.com/me/photos?access_token=" . $FacebookToken;
$url = $url . "&url=" . urlencode("http://www.url.to/the/image.jpg");
$url = $url . "&message=" . urlencode($Description);
$url = $url . "&method=POST";
$data = file_get_contents($url); // Can change to curl if
// file_get_contents is blocked on your host
?>
Here is a short example, recovered from my codes:
$fbPost = curl_init();
curl_setopt($fbPost, CURLOPT_RETURNTRANSFER, true);
curl_setopt($fbPost, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($fbPost, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($fbPost, CURLOPT_POST, true);
curl_setopt($fbPost, CURLOPT_ENCODING, 'gzip');
$photoInfo = array(
'access_token' => <USER_ACCESS_TOKEN>,
'name' => <IMAGE_DESCRIPTION>,
'url' => <ABSOLUTE_URL>/images/photo.jpg',
);
curl_setopt($fbPost, CURLOPT_URL, 'https://graph.facebook.com/<USER_ID>/photos');
curl_setopt($fbPost, CURLOPT_POSTFIELDS, $photoInfo);
$result = json_decode(curl_exec($fbPost), true);
curl_close($fbPost);
Related
I need some help with my Curl GET Request to the Spotify API.
The API has three different ways/endpoints to get an authorization.
I read some articles, to find the correct syntax to send the request. But i always get an error. If i post the url into my brwoser it works perfectly, also with the redirect uri.
But it doesnt work with the Curl GET Request.
It sounds stupid, but i spend the last three days with this Problem.
My code:
<?php
$client_id = 'myClientID';
$redirect_url = 'http://mywebsite/first/page.php';
$scope = 'user-read-private%20user-read-email';
$data = array(
'client_id' => $client_id,
'response_type' => 'code',
'redirect_uri' => $redirect_url,
'state' => stateHash(), // Create a random hash
'scope' => $scope,
'show_dialog' => 'true'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.spotify.com/authorize' . http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
$result=curl_exec($ch);
echo $result;
The error from the API Shows me this:
or i got an "1" as response.
I hope that i get some nice tips :)
There is a package for Spotify web API try using that
composer require jwilsson/spotify-web-api-php
Before using the Spotify Web API, you'll need to create an app at Spotify’s developer site.
Simple example displaying a user's profile:
require 'vendor/autoload.php';
$session = new SpotifyWebAPI\Session(
'CLIENT_ID',
'CLIENT_SECRET',
'REDIRECT_URI'
);
$api = new SpotifyWebAPI\SpotifyWebAPI();
if (isset($_GET['code'])) {
$session->requestAccessToken($_GET['code']);
$api->setAccessToken($session->getAccessToken());
print_r($api->me());
} else {
$options = [
'scope' => [
'user-read-email',
],
];
header('Location: ' . $session->getAuthorizeUrl($options));
die();
}
For more instructions and examples, check out the documentation.
i want detect post id of new post in telegram channel with telegram bot that is admin in channel and forward new post to other channel that I have already specified
i tried to get id of post that bot forwarded to channel with this php code below
function bot($method, $datas = [])
{
$url = "https://api.telegram.org/bot" . API_KEY . "/" . $method;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
$res = curl_exec($ch);
if (curl_error($ch)) {
var_dump(curl_error($ch));
} else {
return json_decode($res);
}
}
$msg_id = bot('ForwardMessage', [
'chat_id' => $channel,
'from_chat_id' => $chat_id,
'message_id' => $message_id
])->result->message_id;
but i need php code that can chek channel if new post send from other admins, robot find post id and forward post to other channel that I have already specified in two channels that robot is admin
i read api bot of telegram there says about how can get post channel in this link: https://core.telegram.org/bots/api#update
in my bot i use setwebhook, which one of $channel_post or $chid is correct?
$update = json_decode(file_get_contents('php://input'));
$channel_post = $update->message->channel_post;
$chid = $update->channel_post->message->message_id;
follow this code :
if( isset($update->channel_post) && $update->channel_post )
{
if(isset($update->channel_post->text) && $update->channel_post->text)
{
$apiToken = "my_bot_api_token";
$data = [
'chat_id' => '#targetChannel',
'text' => $update->channel_post->text
];
file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data) );
}
}
I have created a facebook app in last month.
I am trying to upload a video from my PHP code, but it throws an error that (#353) You must select a video file to upload. While I tried to upload the same video from my Facebook account directly and it gets uploaded properly.
I don't know what is wrong things that exists, PHP code is as below
$api="/me/videos";
$uploaded_videos=$facebook->api($api);
$video_file_path=$user_dir_abs_path."/NewProject20.mov";
if(file_exists($video_file_path))
{
echo "file exists...";
}else{
die("not exist");
}
$ret_obj = $facebook->api('/me/videos', 'POST', array(
'source' => '#' . $video_file_path,
'title' => "This is just a test",
'description' => 'test9000',
'privacy' => json_encode(array('value' => 'EVERYONE' )),
)
);
echo '<pre>'. $ret_obj.'</pre>';
Video I have uploaded is here
Document I refer to code is here
https://developers.facebook.com/blog/post/493/
https://developers.facebook.com/blog/post/608/
I have used following code as well, but I am getting that same error..
$id=$facebook->getUser(); /* UID of the connected user */
$api="/".$id."/videos";
echo "api -> $api";
/*$ret_obj = $facebook->api('/me/videos', 'POST', array(*/
$ret_obj = $facebook->api($api, 'POST', array(
'source' => '#' . $video_file_path,
'title' => "This is just a test",
'description' => 'test9000',
'privacy' => json_encode(array('value' => 'EVERYONE' )),
)
);
echo '<pre>'. $ret_obj.'</pre>';
From your comments, I got to know that you need to upload/post a video from your server to facebook, instead form posting method specified in documentation.
I don't know much about facebook-sdk, I would suggest you to use CURL method instead.
<?php
$app_id = "XXXXXXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXXXXXXXXXXXX";
$my_url = "http://localhost/url_of_this_page.php";
$video_title = "Video title here";
$video_desc = "Video description here";
$code = $_REQUEST["code"];
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
$post_url = "https://graph-video.facebook.com/me/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&". $access_token;
$ch = curl_init();
$data = array('name' => 'file', 'file' => '#'.realpath("ipad.mp4"));// use realpath
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
if( $res === false ) {
echo curl_error($ch);
}
curl_close($ch);
?>
As a response from facebooks, you'l get video id like:
{"id":"892161284142980"}
Have a look at https://developers.facebook.com/docs/graph-api/reference/v2.0/user/videos/#publish
Videos must be encoded as multipart/form-data and published to
graph-video.facebook.com instead of the regular Graph API URL.
If anyone is still having issue uploading videos via the facebook sdk4 api, the below code worked for me:
$file_name = "/absolute/path/to/file/in/directory";
$linkData = [
'source' => new \CURLFile($file_name, 'video/m4v'),
'message' => 'Your message',
];
$ret_obj = $facebook->api('/me/videos', 'POST', $linkData);
Here is the code:
$file= 'bbbb.jpg';
$data = array(
basename($file) => "#".realpath($file),
"caption" => "Uploaded using graph api",
"aid" => '13595',
"access_token" => $accessToken,
'method' => 'photos.upload'
);
$sds =$facebook->api($data);
This is the error
Uncaught CurlException: 26: failed creating formpost data
What to do?
Here are some various ways to upload photos using the Graph API. The examples assume you've instantiated the $facebook object and have a valid session for the current user.
1 - Default Application Album of Current User
This example will upload the photo to your default application album of the current user. If the album does not yet exist it will be created.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $args);
print_r($data);
2 - Target Album
This example will upload the photo to a specific album.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);
3 - Target Album with Access Token
This example will upload a photo to a specific album which requires an access token.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos?access_token='. $ACCESS_TOKEN, 'post', $args);
print_r($data);
your $data array should have "message" instead of "caption",
also, remove "aid", "method", and "access_token"
your $data has to have the file data and "message", that is it.
$sds =$facebook->api('/me/13595/photos', 'POST', $data);
where instead of 13595 just use the variable with the album aid
also, if needed, access_token is best appended to api uri like this:
$sds =$facebook->api('/me/13595/photos?access_token='.$access_token, 'POST', $data);
also, if the php sdk doesn't work for you, I have successfully used cURL instead if your php installation supports it. in that case see cURL example at Upload Photo To Album with Facebook's Graph API
The latest version of the Facebook PHP SDK wont work with the above examples without the following update to the code.
class Facebook {
...
*Line #539*
protected function makeRequest($url, $params, $ch=null) {
if (!$ch) {
$ch = curl_init();
}
if( isset($params['doMultiPart']) ) {
$doMultiPart= true;
unset($params['doMultiPart']);
} else {
$doMultiPart= false;
}
$opts = self::$CURL_OPTS;
$opts[CURLOPT_POSTFIELDS] = $doMultiPart ? $params : http_build_query($params, null, '&');
...
Basically the problem is that the PHP SDK uses "curl_setopt_array" which if you pass it a url encoded string as the option value it will pass the data as application/x-www-form-urlencoded when what you really want is multipart/form-data; to do this we simply switch to passing in the array of options if we have a param of doMultiPart in the params array.
This was a quick hack I put together to get something working, probably need to review the code to make sure it doesnt break anything else you are doing. Otherwise enjoy.
I've set up a Facebook application, I've requested the extended permissions and now I'm trying to upload a photo but it doesn't work!
I've tried everything, from
$facebook->api_client->photos_upload('photo/789165784.jpg');
To
$facebook->api_client->photos_upload('photo/789165784.jpg', NULL, 'My photo', 100000287894654);
I'm beginning to suspect that I need to set up some extensions for php. I'm using WAMP and since the server is currently offline, I can't test it on production until tomorrow (I think..).
Thanks!
Here are some various ways to upload photos using the Graph API. The examples assume you've instantiated the $facebook object and have a valid session for the current user.
1 - Default Application Album of Current User
This example will upload the photo to your default application album of the current user. If the album does not yet exist it will be created.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $args);
print_r($data);
2 - Target Album
This example will upload the photo to a specific album.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);
3 - Target Album with Access Token
This example will upload a photo to a specific album which requires an access token.
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/'. $ALBUM_ID . '/photos?access_token='. $ACCESS_TOKEN, 'post', $args);
print_r($data);
I don't know too much about this, I'm afraid, but the following link suggests that other people have had the same problem. Maybe this forum will be helpful:
http://forum.developers.facebook.com/viewtopic.php?pid=93450
It also links to this page, which seems like it might help:
http://wiki.auzigog.com/Facebook_Photo_Uploads
Ben