Search code examples
phphtmlfile-get-contentsgoogle-translate

Getting Google Translate's html codes in php


after reading these in below, I still can't see the solution : Extract data from website via PHP php extract body tag content

after i using

file_get_contents("https://translate.google.com/#view=home&op=translate&sl=en&tl=fa&text=Help%20Me")

It seems it doesn't contain the words that I typed in box of translator(the words:Help Me)

How can i get all the data from start to end that contains the word that user(myself) typed?


Solution

  • Google Translate uses Javascript to read & display the translated text. It is not available in the source code. You can verify this by viewing the source of the URL you provided yourself (e.g. by copying view-source:https://translate.google.com/#view=home&op=translate&sl=en&tl=fa&text=Help%20Me into your address bar). It's also against their terms of service.

    To access Google Translate programmatically you need to use the Cloud Translation API. This API provides $10/month of free usage, which is enough for most hobbyist projects.

    Here is more information on pricing and quotas, and even a handy guide to using PHP with it.