【发布时间】:2019-07-10 11:05:26
【问题描述】:
当我们向 [Translate API][1] 进行查询时:
function curl($url, $post_array=false){
$handle = curl_init();
if (FALSE === $handle)
throw new Exception('failed to CURL initialize; '. __FILE__);
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
if($post_array) {
curl_setopt($handle, CURLOPT_POST, 1 );
curl_setopt($handle, CURLOPT_POSTFIELDS, $post_array );
}
curl_setopt($handle,CURLOPT_HTTPHEADER,array('X-HTTP-Method-Override: GET'));
$response = curl_exec($handle);
return $response;
}
var_dump ( curl("https://www.googleapis.com/language/translate/v2", ['key'=>$key, 'q[]'=>"hello", 'q[]'=>"world", 'source'=>"en", 'target'=>'ru'] ) );
以错误结束:
{
"error": {
"code": 400,
"message": "Required Text",
"errors": [
{
"message": "Required Text",
"domain": "global",
"reason": "required"
}
]
}
}
如何发送多个q输入文本?如我所见,API 不允许 q[] 类型的数组,而是使用多个 q 参数。但是在php 中,我们不能在数组中多次使用相同的键...
【问题讨论】:
-
文档
https://cloud.google.com/translate/docs/reference/translate建议参数只是q而不是q[],您应该根据需要经常重复.. -
@RamRaider 但 php 不允许数组中有多个相同的键
-
postfields使用字符串而不是数组?
标签: php curl google-cloud-platform google-translate google-translation-api