我正在尝试使用Cloudsight API(图像识别),并在响应时间内看到速度问题。他们的网站宣传了6-12秒的响应时间,但我经常看到响应时间超过20秒。
我想知道这是否是CloudSights端的内容,而这只是响应时间,或者我的代码是由于未经优化或做了不必要的工作而导致这些延迟。
以下是我的PHP代码,它完全可以工作,我可以上传图像并读取/显示响应。我的HTML代码是一个调用此PHP文件的简单表单。
<?php
$filename = $_FILES['fileToUpload']['name'];
$filedata = $_FILES['fileToUpload']['tmp_name'];
$filesize = $_FILES['fileToUpload']['size'];
$filetype = $_FILES['fileToUpload']['type'];
/*$tmpfile = $_FILES['fileToUpload']['tmp_name'];
$filename = basename($_FILES['fileToUpload']['name']);*/
$ch = curl_init("https://api.cloudsightapi.com/image_requests");
//$ch = curl_init("http://requestb.in/131zwlo1");
$postFields = [
"image_request[locale]" => "en-US",
"image_request[language]" => "en-US",
"image_request[image]" => "@$filedata".";filename=@$filename".";type=@$filetype"
];
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: CloudSight [key]", "Content-Type:multipart/form-data"));
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
$token = $result['token'];
$status = $result['status'];
sleep(3);
while($status == "not completed"){
sleep(1);
$cht = curl_init("http://api.cloudsightapi.com/image_responses/$token");
curl_setopt($cht, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cht, CURLOPT_HTTPHEADER, array("Authorization: CloudSight 6-j8FTD-h1ZtnlYgacdSEQ"));
$result = curl_exec($cht);
curl_close($cht);
$result = json_decode($result, true);
$status = $result['status'];
};
$name = $result['name'];
$reason = $result['reason'];
if($name != ""){
echo "\nName: $name";
};
if($reason != ""){
echo "\nReason Skipped: $reason";
};
?>