json decode call photo telegram api - php

i want get photo file id.
file_get_contents('php://input'):
{
"update_id": 399206890,
"message": {
"message_id": 149,
"from": {
"id": 81777999,
"is_bot": false,
"first_name": "#goldenguardbot",
"last_name": "✅",
"username": "amirntm",
"language_code": "en-US"
},
"chat": {
"id": 81777999,
"first_name": "#goldenguardbot",
"last_name": "✅",
"username": "amirntm",
"type": "private"
},
"date": 1507643430,
"photo": [
{
"file_id": "AgADBAADpaoxG6lZ6VIeePTlKxaJsl3X-RkABPbjyThHJgF7FLwBAAEC",
"file_size": 1639,
"file_path": "photos/file_4.jpg",
"width": 90,
"height": 72
},
{
"file_id": "AgADBAADpaoxG6lZ6VIeePTlKxaJsl3X-RkABIrarvPZGVNGFrwBAAEC",
"file_size": 22230,
"width": 320,
"height": 256
},
{
"file_id": "AgADBAADpaoxG6lZ6VIeePTlKxaJsl3X-RkABKFhu79tL5EBF7wBAAEC",
"file_size": 95422,
"width": 800,
"height": 640
},
{
"file_id": "AgADBAADpaoxG6lZ6VIeePTlKxaJsl3X-RkABGzlLqe_Yv0PFbwBAAEC",
"file_size": 172689,
"width": 1160,
"height": 928
}
]
}
}
i want file id how get it?
for example:
$update->message->photo->file_id;

The "photo" section is an array. Square brackets in JSON ([]) indicate an indexed array. Curly braces ({}) indicate an object or associative array, depending on how you choose to parse it in PHP.
You don't mention which "file_id" you want, but assuming you want the first:
$update = json_decode(file_get_contents('php://input'));
echo $update->message->photo[0]->file_id;

You need to decode it to PHP object/array, and get photos in $update->message->photo, that will be array of one photo in different resolution, the latest one always be largest, so you can use end($photos) to get it, and file_id is corresponding file identity.
Code example:
$json = file_get_contents('php://input'):
$update = json_decode($json);
$photos = $update->message->photo;
$photo = end($photo);
$file_id = $photo['file_id'];

Related

how to realize when getting sendPhoto information (by bot Telegram) ,Which channel has been sent from?

My bot is registered in several groups and receives data from them.
when receive the the json code.
{
"update_id": 753984481,
"message": {
"message_id": 158011,
"from": {
"id": 212105015,
"first_name": "\u0634\u0631\u06a9\u062a \u0635\u0628\u0627 \u0645\u0647\u0631 \u0633\u06cc\u0631\u0627\u0641"
},
"chat": {
"id": -196924840,
"title": "\u067e\u0631 \u067e\u0631\u0648\u0627\u0632 \u0635\u0628\u0627 \u0645\u0647\u0631 \u0633\u06cc\u0631\u0627\u0641",
"type": "group",
"all_members_are_administrators": true
},
"date": 1500091212,
"photo": [
{
"file_id": "AgADBAAD9qkxG98UMFNewex76YKoYAr-vBkABEvcu9cjuXx1WCQDAAEC",
"file_size": 1168,
"width": 67,
"height": 90
}
]
}
}
How do I know which message is sent from (Groups)?
as you can see, in your json message.chat.type is equal to "group", when your bot receives a message from a channel, its type value will be equal to "channel".
other difference between channels and groups is that the message.chat.id of channels are bigger (13 digit numbers)

zencoder notifications json

I'm working with some videos on PHP, using zencoder to encode the videos, save them on s3 and then notify my site back when it's all done.
Everything is working until I have to process the notifications returned as json and pull out the new url to the saved video.
this:
$notification = $zencoder->notifications->parseIncoming();
if($notification->job->state == "finished")
{
$encode_id=$notification->job->id;
}
works fine. I just need some pointers on accessing the url.
The notification is sent as:
{
"output": {
"frame_rate": 30.0,
"label": "video_id_",
"total_bitrate_in_kbps": 3115,
"md5_checksum": null,
"channels": "2",
"audio_codec": "aac",
"duration_in_ms": 4225,
"video_codec": "h264",
"url": "http://my_url/597bd3592bf4a9d70f04dc676c44de6d.mp4",
"thumbnails": [{
"label": null,
"images": [{
"url": "http://my_url/_key__0000.png",
"format": "PNG",
"dimensions": "640x360",
"file_size_bytes": 482422
}]
}],
"video_bitrate_in_kbps": 3052,
"width": 640,
"format": "mpeg4",
"height": 360,
"audio_sample_rate": 44100,
"state": "finished",
"audio_bitrate_in_kbps": 63,
"id": 41424918,
"file_size_in_bytes": 1625847
},
"input": {
"frame_rate": 30.0,
"total_bitrate_in_kbps": 3867,
"md5_checksum": null,
"channels": "2",
"audio_codec": "aac",
"duration_in_ms": 4167,
"video_codec": "h264",
"video_bitrate_in_kbps": 3764,
"width": 640,
"format": "mpeg4",
"height": 360,
"audio_sample_rate": 44100,
"state": "finished",
"audio_bitrate_in_kbps": 103,
"id": 22371764,
"file_size_in_bytes": 2028809
},
"job": {
"created_at": "2012-07-14T22:25:08Z",
"test": true,
"updated_at": "2012-07-14T22:25:47Z",
"submitted_at": "2012-07-14T22:25:08Z",
"pass_through": null,
"state": "finished",
"id": 22377083
}
}
but something like: $video_file=$notification->output->url; doesn't.
What am I missing?
If you don't want to use the parseIncoming method...
Use:
$notification = json_decode(trim(file_get_contents('php://input')), true);
as apposed to:
$notification = $zencoder->notifications->parseIncoming();
The second paramater of 'true' will format the results as an array as apposed to an object. From there, you can access all the values like:
$notification['output']['file_size_in_bytes'];
The parseIncoming method will return a stdClass, so referencing values within it is done with:
$notification->key

How to get data(json array) of facebook public photo by using photo url?

I want to get details of public photo of any user's facebook account.
here is my image url : http://www.facebook.com/photo.php?fbid=346729048691589&set=a.346719012025926.83282.100000634900314&type=1&l=f48e02a2e3&permPage=1
and i want details of this photo so i have generated url like : http://graph.facebook.com/346729048691589/albums but getting data as blank array. and also in my album all the photos are public.
I read this url : How to use Facebook Photos in an iPhone App? and example getting the result and i want the same result also but some how i am not getting it.
And i read that we don't require access token if we want to use any public details of facebook but here i am not getting the result.
So Can any one help me?
Thanks
How to get data(json array) of facebook public photo by using photo url?
What I did was to take the fbid from the querystring of your image url in your question. Then I went to the graph API explorer tool and brought it up. See link: https://developers.facebook.com/tools/explorer?method=GET&path=346729048691589
When I did that it returned:
{
"id": "346729048691589",
"from": {
"name": "Development SandBox",
"id": "100000634900314"
},
"picture": "http://photos-c.ak.fbcdn.net/hphotos-ak-snc7/421606_346729048691589_216224001_s.jpg",
"source": "http://sphotos.xx.fbcdn.net/hphotos-snc7/421606_346729048691589_216224001_n.jpg",
"height": 469,
"width": 700,
"images": [
{
"width": 700,
"height": 469,
"source": "http://sphotos.xx.fbcdn.net/hphotos-snc7/s720x720/421606_346729048691589_216224001_n.jpg"
},
{
"width": 180,
"height": 120,
"source": "http://photos-c.ak.fbcdn.net/hphotos-ak-snc7/421606_346729048691589_216224001_a.jpg"
},
{
"width": 130,
"height": 87,
"source": "http://photos-c.ak.fbcdn.net/hphotos-ak-snc7/421606_346729048691589_216224001_s.jpg"
},
{
"width": 75,
"height": 50,
"source": "http://photos-c.ak.fbcdn.net/hphotos-ak-snc7/421606_346729048691589_216224001_t.jpg"
}
],
"link": "http://www.facebook.com/photo.php?fbid=346729048691589&set=a.346719012025926.83282.100000634900314&type=1",
"icon": "http://static.ak.fbcdn.net/rsrc.php/v1/yz/r/StEh3RhPvjk.gif",
"created_time": "2012-02-10T11:14:42+0000",
"position": 2,
"updated_time": "2012-02-10T11:14:43+0000",
"type": "photo"
}

download facebook photos using graph API in JSON Format?

Hi using graph API i am able to get the photos of my albums, now i want to download all the photos in the album to my computer. it returns data in JSon format, how can i filter only the Urls of the images from that json and then download all photos from that url
my json is some like this format
{
"data": [
{
"id": "1140894483853",
"from": {
"name": "Muhammad Asghar",
"id": "1272156814"
},
"tags": {
"data": [
{
"id": "1272156814",
"name": "Muhammad Asghar",
"x": 52.6,
"y": 29.538,
"created_time": "2009-08-20T07:46:50+0000"
}
]
},
"picture": "http://photos-d.ak.fbcdn.net/photos-ak-snc1/v3356/171/103/1272156814/s1272156814_30392128_5890712.jpg",
"source": "http://a4.sphotos.ak.fbcdn.net/photos-ak-snc1/v3356/171/103/1272156814/n1272156814_30392128_5890712.jpg",
"height": 604,
"width": 498,
"images": [
{
"height": 604,
"width": 498,
"source": "http://a4.sphotos.ak.fbcdn.net/photos-ak-snc1/v3356/171/103/1272156814/n1272156814_30392128_5890712.jpg"
},
Please anyone help.
You can use JsonDecode: JsonDecode
assume your json string is in a $data variable, you can try something like this:
<?php
var_dump( json_decode($data));
?>
if you look at the source code of the page you'll have a pretty clear idea of how the json is structured, and you can easly use a foreach statement to get only the urls you need

PHP JSON data foreach problem

I want to make a PHP JSON data foreach, but I met some problem. First: I can not get the properties part. Second it alawys show wrong in line: echo '<div class="image">...
Fatal error: Cannot use object of type stdClass as array in ...
This is the json data:
[
{
"post_id": "504464716_189878284371815",
"message": "\"Happy Birthday to You\" \"Happy Birthday to Mama\"",
"attachment": {
"media": [
{
"href": "http:\/\/www.facebook.com\/photo.php?fbid=493710409716&set=a.453260184716.254996.504464716",
"alt": "",
"type": "photo",
"src": "http:\/\/photos-f.ak.fbcdn.net\/hphotos-ak-snc4\/hs049.snc4\/34793_493710409716_504464716_5821684_2056840_s.jpg",
"photo": {
"aid": "2166659457206182932",
"pid": "2166659457211749620",
"owner": 504464716,
"index": 24,
"width": 225,
"height": 225
}
}
],
"name": "Wall Photos",
"href": "http:\/\/www.facebook.com\/album.php?aid=254996&id=504464716",
"caption": "\"Happy Birthday to You\" \"Happy Birthday to Mama\"",
"description": "",
"properties": [
{
"name": "By",
"text": "Suman Acharya",
"href": "http:\/\/www.facebook.com\/profile.php?id=504464716"
}
],
"icon": "http:\/\/static.ak.fbcdn.net\/rsrc.php\/yD\/r\/aS8ecmYRys0.gif",
"fb_object_type": "album",
"fb_object_id": "2166659457206182932"
},
"action_links": null,
"privacy": {
"value": ""
}
},
...
]
Here is my php code:
foreach ($data as $result) {
echo '<div class="title">'.htmlspecialchars($result->message).'<br />'.htmlspecialchars($result->description).'<br />'.htmlspecialchars($result->caption).'<br />';
if(!empty($result->attachment->properties[0]->text)){
foreach ($result->attachment->properties[0] as $properties) {
echo htmlspecialchars($properties->name).'<br />'.htmlspecialchars($properties->text).'</div>';
}
}
if(!empty($result->attachment->media)){
echo '<div class="image"><img src="'.htmlspecialchars($result->attachment->media[0]->src).'" /><br>'.htmlspecialchars($result->attachment->media[0]->type).'</div>';
}
}
If i were you i would just force the decoding to an assoc array by true as the second arg to json_decode. If you cant or dont want to do that then try accessing it like this:
$result->attachment->media->{0}->href
Use json_decode($the_data, true); instead of json_decode($the_data); that way it will return you an associative array instead of a StdClass.

Categories