Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript][PHP] Współpraca Google Translate API z PHP, Jak wrzucić wynik tej funkcji do zmiennej php?
starter91php
post
Post #1





Grupa: Zarejestrowani
Postów: 75
Pomógł: 0
Dołączył: 6.05.2009

Ostrzeżenie: (0%)
-----


Witam, zająłem się ostatnio używaniem Google Translate API. Nie umiem go jednak skonfigurować ze swoim skryptem php.
Przykładowe wykorzystanie tego API wygląda tak:
[php]
[JAVASCRIPT] pobierz, plaintext
  1. <html>
  2. <head>
  3. <title>Translate API Example</title>
  4. </head>
  5. <body>
  6. <div id="sourceText">dog</div>
  7. <div id="translation"></div>
  8. <script>
  9. function translateText(response) {
  10. document.getElementById("translation").innerHTML += "<br>" + response.data.translations[0].translatedText;
  11. }
  12. </script>
  13. <script>
  14. var newScript = document.createElement('script');
  15. newScript.type = 'text/javascript';
  16. var sourceText = escape(document.getElementById("sourceText").innerHTML);
  17.  
  18. var source = 'https://www.googleapis.com/language/translate/v2?key=API-KEY&source=en&target=pl&callback=translateText&q=' + sourceText;
  19. newScript.src = source;
  20.  
  21. // When we add this script to the head, the request is sent off.
  22. document.getElementsByTagName('head')[0].appendChild(newScript);
  23. </script>
  24. </body>
  25. </html>
[JAVASCRIPT] pobierz, plaintext
/php]

W tym momencie, na górze strony pod napisem dog, pojawia się napis: pies : )

Wpisanie adresu ze zmiennej source (https://www.googleapis.com/language/translate/v2?keyAPIKEY&source=en&target=de&callback=translateText&q=dog) do przegladarki daje taki tekst:

  1. // API callback
  2. translateText({
  3. "data": {
  4. "translations": [
  5. {
  6. "translatedText": "Hund"
  7. }
  8. ]
  9. }
  10. }
  11. );


Moje pytanie brzmi, mam zmienna $eng = 'tekst po angielsku';
i chcialbym z tego uzyskać zmienną $pl = 'przetlumaczony przez api tekst'.
Jak to zrobić?
Go to the top of the page
+Quote Post
Sephirus
post
Post #2





Grupa: Zarejestrowani
Postów: 1 527
Pomógł: 438
Dołączył: 28.06.2011
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


to co Ci wypluwa JSONem API przemiel przez funkcję json_decode i odpowiednio wyciagnij (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post
starter91php
post
Post #3





Grupa: Zarejestrowani
Postów: 75
Pomógł: 0
Dołączył: 6.05.2009

Ostrzeżenie: (0%)
-----


dzieki : )

Ten post edytował starter91php 5.03.2012, 11:08:19
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.12.2025 - 11:50