Yahoo API "Insufficient Parameters" Error

Asked 1 years ago, Updated 1 years ago, 401 views

I'm trying to run a Japanese morpheme analysis of Yahoo API.

Yahoo API Japanese Morpheme Analysis

The code does not seem to be a problem, but an "Insufficient Parameters" error occurs.

In addition to the above official URL, I have also referred to this site.
Morphology analysis with Yahoo! API

The code is as follows:

<?php

class YahooApiParse
{
    constEND_POINT="https://jlp.yahooapis.jp/MAService/V2/parse";
    const APP_ID="(my API id)";
    const JSONRPC = "2.0";
    const METHOD = "jlp.maservice.parse";

    public function parse($text)
    {
        // Log the text being parsed
        $this->log($text);

        // Parse the text
        $result=$this->parseText($text);

        // Log the result of the parse
        $this->log($result);

        return$result;
    }

    private function parseText($text)
    {
        // $param's 'params' should be an object
        $param=[
            'id' = > time(),
            'jsonrpc' = > self::JSONRPC,
            'method' = > self::METHOD,
            'params' = > ['q' = > $text],
        ];

        var_dump($param);
        // Parse the text using the Yahoo API
        $curl=curl_init();
        curl_setopt($curl,CURLOPT_HTTPHEADER,array(
            'Content-Type: application/json',
            'User-Agent: Yahoo AppID:'.self::APP_ID
        ));
        curl_setopt($curl,CURLOPT_URL,self::END_POINT);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl,CURLOPT_POST,true);
        curl_setopt($curl,CURLOPT_POSTFIELDS,$param);
        $json=curl_exec($curl);
        curl_close($curl);

        return json_decode($json, true);
    }

    private function log($text)
    {
        // Log the text somewhere
    }
}

Error Output

<b>Warning</b>: Array to string conversion in<b>C:\web\text\class\YahooApiParse.php</b>online<b>45>/b>br/gt;>
array(3){
  ["error"] =>
  array(2){
    "code" = >
    int(-32600)
    ["message"] =>
    US>//#string(15) "Invalid request"
  }
  ["id"] = >
  Null
  ["jsonrpc"] = >
  US>//#string(3) "2.0"
}

Which part is the problem?
No matter how many times I look back, I can't find the location of the problem.
I would appreciate it if you could help me.

Thank you for your cooperation.

php api

2023-02-10 09:48

1 Answers

The reason was that the array was sent as it was.

Resolved by adding json_encode as follows:
curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($param));

By the way, I realized thanks to chatGPT.
When I threw in the code, they pointed it out above.
Awesome


2023-02-10 12:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.