Failure to connect to IBM Language Translation via PHP Curl

Asked

Viewed 27 times

0

I have a problem that I can’t solve. Basically I need to translate a string into Português for English. This is the code I was able to develop:

<?php
    class CtrlTranslate{
        public function translate($text) {
            $url = "https://gateway.watsonplatform.net/language-translation/api";

            $user = "[user]";
            $password = "[password]";

            $post_args = array(
                'model_id' => 'pt-en',
                'text' => $text
            );

            $handle = curl_init($url);
            $header_args = array(

                'Content-Type: text/plain',
                'Accept: application/json'
            );

            curl_setopt($handle, CURLOPT_POST, true);
            curl_setopt($handle, CURLOPT_HTTPHEADER, $header_args);
            curl_setopt($handle, CURLOPT_POSTFIELDS, $post_args);
            curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($handle, CURLOPT_USERPWD, $user.":".$password);
            curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
            $response = curl_exec($handle);
            $responseDecoded = json_decode($response, true);
            curl_close($handle);

            return $responseDecoded;
        }
    }
?>
  • Could you translate your question? Note that you are on Stack Overflow in English.

  • Oops, sorry, buddy.

  • I have a problem that I can not solve. Basically I need to translate a text from Portuguese to English. This is the code I was able to make.

  • Use [Edit] to translate the question

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.