首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将此cURL转换为wp_remote_*?

如何将此cURL转换为wp_remote_*?
EN

WordPress Development用户
提问于 2019-09-27 19:53:20
回答 2查看 2.2K关注 0票数 1

我下面的函数包括一个cURL调用,它在中抛出一个文本字符串,返回一个响应对象,然后我们解析该对象以获得一个特定的片段。

有人建议我将cURL PHP语句转换为使用WordPress‘wp_remote_get

我已经阅读了文档,但老实说,我不明白我的cURL头字段应该如何映射到wp_remote_get参数。或者即使它应该是wp_remote_post而不是wp_remote_get

我怎么才能试着理解这里该做什么呢?

代码语言:javascript
运行
复制
    function get_entity_type(
        $text_to_analyse,           // passed string to be handed to GCloud NLP
        $entity = 'type'            // part of each "entities" result to return
    ) {

        // Google Cloud API key
        $options = get_option( 'cxt_settings' );
        $google_nlp_api = $options['cxt_gcloud'];




        // Supply data payload in JSON format
        $data = '{
          "document":{
            "type":"PLAIN_TEXT",
            "content":"'.$text_to_analyse.'"
          },
          "encodingType":"UTF8"
        }';
        $payload = $data;

        // Call the API endpoint, with API key
        $url = 'https://language.googleapis.com/v1/documents:analyzeEntities?key='.$google_nlp_api;

        // Prepare to get results using cURL
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        // Set HTTP Header for POST request
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($payload))
        );

        // Submit the POST request
        $result = curl_exec($ch);

        // Store result in array
        $arr = json_decode($result, true);

        // Close cURL session handle
        curl_close($ch);




        // Pluck out the first value from the response object
        $ent_val = $arr['entities'][0][$entity];

        return $ent_val;
        // List of possible entities: https://cloud.google.com/natural-language/docs/reference/rest/v1/Entity#Type
        // UNKNOWN
        // PERSON
        // LOCATION
        // ORGANIZATION
        // EVENT
        // WORK_OF_ART
        // CONSUMER_GOOD
        // OTHER

    }
EN

回答 2

WordPress Development用户

发布于 2019-10-10 14:14:59

代码语言:javascript
运行
复制
/* ########################################################################## *
 *
 *   DETERMINE ENTITY, via native wp_remote_post() call
 *
/* ########################################################################## */

function get_entity_type_via_wp(
    $text_to_analyse,           // passed string to be handed to GClouD NLP
    $entity = 'type'            // part of each "entities" result to return
) {

    // Google Cloud API key
    $options = get_option( 'cxt_settings' );
    $google_nlp_api = $options['cxt_gcloud'];



    // Call the API endpoint, with API key
    $url = 'https://language.googleapis.com/v1/documents:analyzeEntities?key='.$google_nlp_api;

    // Request payload
    $payload = '{
      "document":{
        "type":"PLAIN_TEXT",
        "content":"'.$text_to_analyse.'"
      },
      "encodingType":"UTF8"
    }';


    // Call Goolge NLP API via wp_remote_post();
    // cf. https://wordpress.stackexchange.com/questions/349271/how-to-convert-this-curl-to-wp-remote?noredirect=1#comment510738_349271
    //
    $result_full = wp_remote_post(
        $url,
        array(
            'method'      => 'POST',
            'timeout'     => 45,
            'redirection' => 5,
            'httpversion' => '1.0',
            'blocking'    => true,
            'headers'     => array(
                'Content-Type' => 'application/json; charset=utf-8'
            ),
            'body'      =>  $payload,                       // Payload, text to analyse
            'data_format' => 'body'
        )
    );


    // Just the "body" bit
    $result_entities = $result_full['body'];

    // Store result in array
    $arr = json_decode($result_entities, true);

    // Pluck out the first value from the response object
    $ent_val = $arr['entities'][0][$entity];

    return $ent_val;


    // return $ent_val;
    // List of possible entities: https://cloud.google.com/natural-language/docs/reference/rest/v1/Entity#Type
    // UNKNOWN
    // PERSON
    // LOCATION
    // ORGANIZATION
    // EVENT
    // WORK_OF_ART
    // CONSUMER_GOOD
    // OTHER

}
票数 0
EN

WordPress Development用户

发布于 2023-04-13 07:26:54

你为什么要这么做?

由php和curl创建的WordPress在WordPress中工作得很好。卷发非常强大,有更多的选择。

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/349271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档