1

My function below includes a cURL call which throws a text string at a Google Cloud text analysis API, returning a response object, which we then parse to get a specific piece.

It has been recommended to me that I switch the cURL PHP statement to use WordPress' wp_remote_get.

I have read the docs for that but, to be honest, I don't understand how my cURL header fields should map to wp_remote_get arguments. Or even if it should be wp_remote_post rather than wp_remote_get.

How can I try and understand what to do here?

    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

    }
1

2 Answers 2

0
/* ########################################################################## *
 *
 *   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

Why do you do this?

WordPress created by php and curl works fine in WordPress. Curl is very powerful and has more options

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.