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