php通过CURL模拟post提交请求

 <?php
header("Content-type:text/html;charset=utf-8"); class Test{ public function request_post($url = '', $param = '',$headers = array()){
if (empty($url) || empty($param) || empty($headers)) {
return false;
} $postUrl = $url;
$curlPost = $param;
$curlHeader = $headers;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeader);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, );//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, );//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch); return $data;
} public function testAction(){
$url = 'http://web.zhimabot.com:8080/parsor9/robot';
$post_data['app_id'] = '';
$post_data['client_id'] = '';
$post_data['session_id'] = '';
$post_data['intent_update'] = '';
$post_data['query'] = '播放刘德华的忘情水'; $headers = array();
array_push($headers, "Content-Type: application/json; charset=utf-8","Accept: application/json");
$post_data = json_encode($post_data,JSON_UNESCAPED_UNICODE); $res = $this->request_post($url, $post_data,$headers);
return $res;
} } function fn(){
$action = new Test();
$result = $action->testAction();
var_dump($result);
} fn(); ?>
上一篇:linux上常见的压缩解压缩的命令


下一篇:uva 674 Coin Change 换钱币【完全背包】