ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • dialogflow - curl 호출 php 스크립트
    프로그래밍/자연어처리 2018. 4. 4. 13:27
    반응형




    웹페이지에서 직접 dialogflow 를 사용할 일이 있어서 

    php curl 로 dialogflow 서비스를 호출하는 스크립트를 작성했다



    dialogflow 참고 사이트 




    참고로 터미널에서는 아래와 같이 curl 호출을 할 수 있다. 


    curl 'https://api.dialogflow.com/v1/query?v=20170712&query=test&lang=ko&sessionId=95aadeec-*** 78dd939&timezone=Asia/Seoul' -H 'Authorization:Bearer 9***'






    여기서 본인의 인증 코드를 모르는 사람은 

    dialogflow 서비스의 설정의 General 탭에서 확인 가능하다. 











    링크한 문서의 access token을 보면 query 질의에는 client access token 을 사용하면 된다고 한다. 




    참고로 access token 설정이 잘못 될 경우 

    아래와 같은 에러 메세지가 나온다. 





    {"status":{"code":401,"errorType":"unauthorized","errorDetails":"You are not authorized for this operation. Invalid access token"}}






    아래는 전체 php 스크립트이다. 




    <?

    // Get cURL resource

    $curl = curl_init();


    // Set some options - we are passing in a useragent too here

    $accesstoken = "9***";

    $developertoken = "9****";

    $headr = array();

    $headr[] = 'Content-type: application/json';

    $header[] = 'Authorization:Bearer '.$developertoken;

    $query = "test";

    curl_setopt_array($curl, array(

        CURLOPT_RETURNTRANSFER => 1,

        CURLOPT_URL => 'https://api.dialogflow.com/v1/query?v=20170712&query='.$query.'&lang=ko&sessionId=***-b320c78dd939&timezone=Asia/Seoul',

        #CURLOPT_USERAGENT => 'Codular Sample cURL Request',

        CURLOPT_HTTPHEADER => $header

    ));



    // Send the request & save response to $resp

    $resp = curl_exec($curl);


    // Close request to clear up some resources

    print_r($resp);


    curl_close($curl);

    ?>




    터미널에서 스크립트를 실행할 경우 

    아래와 같이 정상적으로 dialogflow 실행  결과가 나온다. 




    php client.php

    {

      "id": "****",

      "timestamp": "2018-04-04T02:55:07.413Z",

      "lang": "ko",

      "result": {

        "source": "agent",

        "resolvedQuery": "test",

        "action": "s**",

        "actionIncomplete": false,

        "parameters": {},

        "contexts": [

          {

            "name": "test-followup",

            "parameters": {},

            "lifespan": 2

          }

        ],

        "metadata": {

          "intentId": "***",

          "webhookUsed": "true",

          "webhookForSlotFillingUsed": "false",

          "webhookResponseTime": 1297,

          "intentName": "test"

        },

        "fulfillment": {

          "speech": "Hello from webhook",

          "source": "agent",

          "displayText": "Hello from webhook",

          "messages": [

            {

              "type": 0,

              "speech": "Hello from webhook"

            }

          ]

        },

        "score": 1.0

      },

      "status": {

        "code": 200,

        "errorType": "success",

        "webhookTimedOut": false

      },

      "sessionId": "***dd939"

    }




    728x90
    반응형
Designed by Tistory.