| 14 | // limitations under the License. |
| 15 | |
| 16 | function http_post($url, $data) { |
| 17 | // array to json string |
| 18 | $data_string = json_encode($data); |
| 19 | $ch = curl_init($url); |
| 20 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); |
| 21 | // post data 封装 |
| 22 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); |
| 23 | // true是获取文本,不直接输出 |
| 24 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 25 | // 强制curl不使用100-continue |
| 26 | curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); |
| 27 | // set header |
| 28 | curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
| 29 | 'Content-Type: application/json', |
| 30 | 'Content-Length: ' . strlen($data_string)) |
| 31 | ); |
| 32 | // 执行 |
| 33 | $result = curl_exec($ch); |
| 34 | curl_close($ch); |
| 35 | return $result; |
| 36 | } |
| 37 | |
| 38 | //key value 数组,如果多,后面用逗号分开key =>value ,key1 => value1 ,.... |
| 39 | echo http_post('http://127.0.0.1:8010/BuiltinTestEchoService/inference', array("a" => 1, "b" => 0.5)); |