How can I save an image received from a Webhook? (WhatsApp API) - php

So, I'm starting to understand a bit more of WhatsApp API and all the messages that my number received are sent to my server via Webhook. The text messages are fine, but I'm struggling with the media messages.
When the user sends an image for example, Facebook's Webhook only sends me the mime_type and the sha256 of the image.
Can anyone please guide me the steps of what I need to do?
Do I need to convert it to base64, and then write the file in my server? Do I need to use a specific function? Do I need to use another programming language that's not PHP?
I'm totally lost on this one.

The way to do this, as pointed out by #CBroe is to use the media endpoints.
Assuming message is the message from the Webhook
// Get the image information, including the download link
$image_url_handle = fopen("https://graph.facebook.com/v13.0/" . $message->id);
$image_url = "";
// Read all of the data before parsing
while (!feof($image_url_handle)) {
$image_url .= fread($image_url_handle);
}
// Close the stream
fclose(image_url_handle);
// Get the url from the image information
$image_url = json_decode($image_url)->url;
// Start a request to the download URL.
$image_stream = fopen($image_url);
Note: There is no error handling or frameworks, though this should work most of the time

Related

Make google drive fmt stream map stream links works when load on server

When I attempt to access video links on my google drive via my server, using via fmt map, they are not working on the client side because of ip tracking.
I have researched all stackoverfow similar topics but there is no solution.
you can check this for reference :
https://www.digitalocean.com/community/questions/google-get-video-fmt-stream-map
I know it's all about ip adresse so how some people make this works ?
Regards.
This answer is not the final solution but it can hopefully bring some new info.
I have tried two ways already. One PHP from the site you suggested and one Javascript.
PHP gives me a link that has similar parameters as a working link that I got from browser developer tools so it could work. The problem as you say is IP because through PHP there is IP of the server and the rest is generated upon that. So I have tried to stream it through PHP to use the same IP address by readfile()
/*code follows code from the link*/
$gdrive = new gdrive;
$drive_link = 'https://drive.google.com/open?id=videoID';
$gdrive->getLink($drive_link);
$theFile = json_decode( $gdrive->getSources() , true );
//echo $theFile[0]['src'];
/*header('Content-Type: video/mp4');
header('Accept-Ranges: bytes');
header('Content-Length:'.filesize($theFile[0]['src']));*/
readfile($theFile[0]['src']);
but it doesn't work anyway. I don't know if that is right way or if there is another google security obstacle?
From Javascript, when trying to request docs.google.com/get_video_info or drive.google.com/file/d/videoID/view, it gives me a CORS Cross-Origin error (in Web Console) that I can't resolve.

Gmail api php reply and include original attachments

I am trying to send a "reply" message using Gmails API and PHP. I can send the message, and even upload documents to send with the message, but I can find ZERO articles, docs, zip, nothing on how to reply to a message using Gmails API and including the original documents. If you have any suggestions, please advise. Here is my current code:
$gMail = new \Google_Service_Gmail($googleClient);
$msg = new \Google_Service_Gmail_Message();
$msg->setThreadId($threadId);
$messageObj = $gMail->users_messages->send("me", $msg);
There is a ton of code as well that I have not included because it doesn't need to be. Best case, if anyone could point me to the docs that explain how to attach original files (without having to download them and re-upload them to the email), this would be great.
Thanks.

Streaming video from server to mobile using base64

We are building an application that deals with images and videos and the privacy requirement is high , where users are not allowed to gain access to the images and videos at all time (privacy option) ,
therefore we went with the option of having a php api that clients request the file and the api returns a base64 encoded response that the client decode and display ,that is for image side ,as for video we are having a trouble finding the right logic .
does VideoView in android helps me achieve this ?
does the api needs to send the video in chunks instead of one large base64 ?
is base64 even right for this requirement ,noting that user should not have direct access to the file at all times .
php api function :
function viewFile($data) {
$file = file_get_contents($data['file_path']);
$mime = mime_content_type($data['file_path']);
return ['status' => 200, 'file' => ['mime' => $mime, 'base64' => base64_encode($file)]];
}
Security with base64? That does not exist. I made a text (in Portuguese) that might help you. Read.
I do not recommend using a very large base64. The server will have a lot of load, the download will be slow, etc.
The ideal is to divide it into smaller pieces. For this you can use bento4 e o ExoPlayer.
For images, I recommend using Cipher. More information for Android and More information for PHP
I made this code to encrypt images. It is already quite old, but I think I can help you enteder this issue.
https://github.com/valdeirpsr/estudo-openssl/blob/master/library/OpensslEncrypt/OpensslEncrypt.php

PHP File get contents give 404 error but exist

In my script I am using a Google YouTube API to receive avatar images from a YouTube channel. Every avatar is received and saved to our own server, except the 'standard' avatars.
This is a screen shot of the standard avatar.
I am trying to save them with file_get_contents, but it gives me an error.
The code:
$filename = './img/avatars/'. $ytname .'.jpg';
if (file_exists($filename)) {
print_r("http://youtube-top.eu/img/avatars/".$ytname.".jpg");
} else {
$url = file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=brandingSettings,snippet&forUsername=".$ytname."&key=#KEYISPRIVATE#");
$json = json_decode($url, true);
$str = $json['items'][0]['snippet']['thumbnails']['default']['url'];
$str = str_replace('https://', 'http://', $str );
$content = file_get_contents($str);
?>
/*
<?php
echo $str;
?>
*/
<?php
file_put_contents('./img/avatars/'. $ytname . '.jpg', $content);
print_r("http://youtube-top.eu/img/avatars/".$ytname.".jpg");
}
}
With channels with a custom avatar, it saved to our server, only not for the standard avatars. This is the error:
file_get_contents(http://i.ytimg.com/i/MJJbPVnNlvRAWSgfVuzxJA/1.jpg):
failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
The image link in the error does exist.
Does someone understand this?
Google has some magic in place to block automated requests, hotlinking, etc. If their watchdog algorithms have flagged activity from your IP, they could be blocking you.
You might try using cURL functions and setting the User-Agent string to something reasonable for a modern browser. Otherwise, I'd suggest rethinking your logic for the app.
Source: personal experience; I can't use Google to automatically determine the nature of 2nd-hand office machines for one of our applications.
Google has some magic in place to block automated requests,
hotlinking, etc. If their watchdog algorithms have flagged activity
from your IP, they could be blocking you.
But why dont dont block me for the custom avatars, and only for the standard avatar ?
and when i do:
<img src="http://i.ytimg.com/i/MJJbPVnNlvRAWSgfVuzxJA/1.jpg" />
its show correctly the image. So google is not blocking the ip of my server..

Video Recording from Browser using Flash, PHP, Red5

I wish to build an application using which I can record video (along with audio) and also audio (only audio preferably in mp3 format).
From some research I did, I found I need a client app in flash or flex, a RTMP Server (RED5 preferable as its free)
This is the code which I used to get cam working flash.
var camera:Camera = Camera.getCamera();
var video:Video = new Video();
video.attachCamera(camera);
addChild(video);
The problem is, I don't know how to send the stream to RED5.
Also, what do I need to do so that I can store the video according to the user. The website I am creating is in PHP/MySQL and need to have their own videos and audios recorded. I love the way facebook has integrated Video Recording.
Check this: http://www.actionscript.org/resources/articles/615/2/Getting-started-with-red5-server/Page2.html
It explains how to connect and use RED5 and gives you an example.
Here's the exact AS3 code for publishing video from Flash to a media server like Red5, Wowza or AMS:
//init vars
public var nc:NetConnection;
public var ns:NetStream;
//net connection to media server
nc = new NetConnection();
nc.connect("rtmp://yourmediaserver/oflaDemo/instance");
//net stream through which the recording data is sent
ns = new NetStream(nc)
//attach cam and mic to net stream
ns.attachCamera(Camera.getCamera())
ns.attachAudio(Microphone.getMicrophone())
//send the data to the media server
ns.publish("streamName","record");
For just audio comment the ns.attachAudio line .
Flash Player can't encode mp3 sound (it can decode). You'll get sound encoded with NellyMoser ASAO. Speex is also an option. See this answer for more details.
oflaDemo is a Red5 app that supports video recording that's shipped with Red5.
For a (commercial) Flash/HTML video recording solution that supports Red5 and PHP you should check out https://hdfvr.com.
Also, what do I need to do so that I can store the video according to the user.
Just execute a PHP script (from the Flash client) that saves the info in the database. You can use POST or GET to send the video data and sessions or cookies to retrieve the user data.
var video:Video;
var camera:Camera = Camera.getCamera();
camera.addEventListener(ActivityEvent.ACTIVITY, active);
video = new Video();
video.attachCamera(camera);
function active(event:Event):void
{
addChild(video);
camera.removeEventListener(ActivityEvent.ACTIVITY, active);
}

Categories