$sample); $labels[] = $label; unset($x); unset($buffer); unset($ids); unset($sample); unset($label); } if (!feof($handle)) { echo "Unexpected fgets() fail"; return -1; } fclose($handle); } function &http_connect($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // true是获取文本,不直接输出 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 强制curl不使用100-continue curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); // set header curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) ); return $ch; } function http_post(&$ch, $data) { // array to json string $data_string = json_encode($data); // post data 封装 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); // set header curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Length: ' . strlen($data_string) ) ); // 执行 $result = curl_exec($ch); return $result; } if ($argc != 2) { echo "Usage: php text_classification.php DATA_SET_FILE\n"; return -1; } ini_set('memory_limit', '-1'); $samples = array(); $labels = array(); read_data($argv[1], $samples, $labels); echo count($samples) . "\n"; // key value 数组,如果多,后面用逗号分开key =>value ,key1 => value1 ,.... $ch = &http_connect('http://127.0.0.1:8010/TextClassificationService/inference'); $count = 0; for ($i = 0; $i < count($samples) - BATCH_SIZE; $i += BATCH_SIZE) { $instances = array_slice($samples, $i, BATCH_SIZE); echo http_post($ch, array("instances" => $instances)) . "\n"; } curl_close($ch); ?>