Transcript for:
Lecture on Google Translate API with PHP and cURL

hey everyone this is Gilbert with interactive Utopia and this week we're going to be looking at the Google translate API all right we're going to do an example with PHP and curl uh pretty simple if you've been looking at the videos from before and this is pretty much what it's going to look like we are going to provide a string that it's going to be in English and the API is going to provide us with a translation which is going to be on Spanish okay so let's get started foreign well I would like to start by introducing myself my name is Gilbert and I'm with interactive Utopia if you need any help with with your web development Projects please do feel free to reach out if you like this video please give it a big thumbs up and if you have any questions please leave them in the comment box below and of course don't forget to subscribe to my channel so let's get started uh all of the code it's going to be on my GitHub if you need to go to uh if you need the code go to github.com forward slash Gilberto Cortez you're going to find the project called Google apis with PHP and all the code from this week and the past week it's gonna be on there all right so uh just in case you need that and like always the remember there is documentation and what we're going to be doing there's reference guides uh there's quick starts uh I'm gonna post the links on the description down below so if you need any help uh you know definitely go over there and go go to the documentation and read a little bit more on that so uh let's get started by opening up visual studio code and uh I'm gonna pretty much go through the code like every week all right if you have any questions again don't don't forget to leave them in the box below and I'll try to help you out the best that I can um like always this is going to be very similar to the past weeks that we've been working on with the Google apis uh we're using our curl class that was made on week one and uh and uh yeah so basically we are you know starting the the uh the code with starting the server session that's where we stored all of our information um you know when when we go to Google and log in we need we get those credentials so we have them stored on the um on the server session so that we can use it throughout the different files all right so if you got questions go to the video from before and you'll be able to find them right there um uh after that we are telling PHP to display all errors so if there's any problems if there's a semicolon missing or there's some variable missing something like that it's going to display it to us it's good right now because we're developing uh so if there's any bugs we need to find them quickly uh so maybe not so good for an everyday application something that's already published but uh for right now you know I would recommend it highly uh and we go ahead and include or require better set the curl class uh which again we I created before uh it just including it in the file and then we are starting a new uh object I'm using the O right here uh we're starting a new object for that curl server and we're passing it the Google access tokens which we retrieved again once we made that log log in one very important thing to mention um on the Link login you're going to have to modify the the Scopes a little bit um I believe it's going to be right here yes so uh we are already using the cloud platform scope uh if you don't have it you know definitely add it but this is the one we're going to be needing Cloud translation so uh don't forget to change it when you create the uh login request that we're gonna that you're gonna be needing that extra scope in there that way you are authorized to use it and also don't forget to go to the Google Cloud console and um enable the API for the uh Cloud translation okay uh the two very important points if not it's not gonna work um going back to visual studio code uh once we started or initiated that object of the curl server then we need to set up the parameters to make the call that we're going to be needing so we need to communicate with the Google translate API so we need to create the Json that we're going to be sending on the last files I just kind of posted the Json in there like on a string and we modified it a little bit this way this week we are going a step uh we're taking an additional step and we're using PHP to encode that Json um string for us if you can call it why am I doing that because when you are sending the request you can ask for multiple strings so we can put all the strings in an array and then request that API you know to translate them for us but if we want to use them later on you know if it's just on the string we can't really work with them but in this scenario because it's in a standard class we can work with it as long as we don't overwrite it so what I'm doing right here is you know I'm creating the the queue pretty much means the strings uh and you can go in the documentation to find all this information the target language uh Spanish uh the source language English and the format it's text we're just providing text you can do text or HTML and these last two ones they're only optional so if you don't to have them it's not that big of a deal it's just uh you you specify what you're sending over to get the best results possible uh for the languages you can go to the documentation if we go back here right here where it says language support and then it'll give you all the different languages that that we can use with it so let's go back one step again the queue that's just the input to the track text to translate Target the format The Source um language again and uh you can select the model that you want to use or if you want to use well this one it's not really per training because you we are using or2 to authenticate this is just in case you are doing it without the proper authentication for the model when you go to the advanced API then you can use that we're just using the basic API right now but there's ways to where you can use your own dictionary so let's say that you're in a technical field and certain words translate a certain way you can do a custom dictionary to translate them so that's I think that's pretty cool but again for right now we're just focusing on working with the API so we're going you know the most basic possible and then once where you provide that information it's gonna give was a Json response back which pretty much has the um the translation on there and there and on top uh this is the URL that we need to send the request to using a post all right so let's go back here so um again we have this the strings we want translated the target language The Source language the format that it's in and this right here it was gonna turn it into Json PHP is going to encode it for us it's a pretty simple you know I'm just I'm making a new variable because as I mentioned we're going to be reusing this information in the future uh so that just create a new variable called Json parameters uh encoding this object where we have all the information and then uh right here is where this line is where we make the request the post request to the URL that we found on the on the documentation and we're passing the Json parameters that we created or PHP created for us using the object we created uh so that's pretty much about it we get a response we store the response and that we can work with it um what I do in my example uh I have an HTML display code which is this section right here it's just basically a small container uh with a title tag and then I'm using a for each Loop to Loop through the um receive the information that way I can display it in a nicer way so you can pretty much do it in tables uh containers dips you know whatever you want to do uh maybe you're not even displaying it maybe uh with something to be stored I don't know uh but definitely you know that's kind of how you work with their results so I just wanted to show you you just need to look through them and then you can just display it as you want and then at the bottom it's just some basic CSS styling you know kind of breaking it up the page and organizing it this you don't need at all the HTML you don't need it all uh so from here on it's just I'm sorry all this bottom section it's just for displaying it so if it's confusing don't even worry about that um I'm gonna show you actually so right here we have this debugging section that I usually leave and uh I'm gonna uncomment that out and then we're just gonna erase all this really quick I'm gonna save it it uploads to my server let's go back to the example we're going to refresh it and there we go so we don't have the HTML side of it we just have the uh the Json that we received and basically we receive the data with translations uh spot zero spot one and then that's the translation it doesn't give you the original text so that's why as I mentioned usually like if you're gonna reuse it you wanna keep it like set it aside that way um you can kind of work with it as I mentioned as we are doing in my example so I'm just gonna go and do undo and do again commenting out commenting it out I'm gonna go ahead and save it we're going back and then refreshing so that's how we displace basically if this is the H1 tag HD tag just a little P tag uh breaker bar this is the translation number one uh with the original another breaker bar translation number two and then the original how do I go back and get the original so when you're looping it you get the key uh you know on your Loop of course and then you get the the translated text so you pause the translated text that you're receiving but then you're going back to the original parameters uh Q which is the strings and then you're using the key for that translated object to reference the original um array and that way you can kind of post it that way cross reference in that way and uh that's pretty much about it it's a simple example it's a basic example hopefully we I didn't make it too complicated trying to make it a little bit more fancy uh but again if you have any questions just feel free to reach out and I'll try to help you out the best that I can um there's also if you have questions about Json and code you can go to the PHP documentation and it can tell you all the information that you want to know about it basically as I mentioned it returns a string containing the Json representation of the supplied value uh if the parameter is an array or an object it will be serialized recursively so that pretty much means you know we use the object and then we are creating the Json representation so that we can send it over uh in a way it's good because it you know it does certain things to make sure that the Json it's uh encoded properly so that way you you minimize the amount of errors that you can have but it's not really needed you can just do a string with the Json and and send that over and that's you know that's pretty much all that you need um and again any you know issues go to the documentation there's a ton of information in there uh and uh and yeah you know so hopefully that helped out I'm just gonna go back to this page right here and again if I'm Gilbert with interactive Utopia if you like this video please do give it a thumbs up subscribe to the channel and if you have any questions leave them in the comment box below and I'll try to help you out the best that I can thank you again and have a great day [Music]