我下面的函数包括一个cURL调用,它在中抛出一个文本字符串,返回一个响应对象,然后我们解析该对象以获得一个特定的片段。
有人建议我将cURL PHP语句转换为使用WordPress‘wp_remote_get。
我已经阅读了文档,但老实说,我不明白我的cURL头字段应该如何映射到wp_remote_get参数。或者即使它应该是wp_remote_post而不是wp_remote_get。
我怎么才能试着理解这里该做什么呢?
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
}发布于 2019-10-10 14:14:59
/* ########################################################################## *
*
* 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
}发布于 2023-04-13 07:26:54
你为什么要这么做?
由php和curl创建的WordPress在WordPress中工作得很好。卷发非常强大,有更多的选择。
https://wordpress.stackexchange.com/questions/349271
复制相似问题