如何用curl 模拟 http post 与C++服务器交互?代码如下:
function pr_post_curl($name = '', $post_data = array(), $josn_type = '1')
{
if (empty($url) || empty($post_data)) {
return false;
}
if ($josn_type == 0) {
$o = "";
foreach ($post_data as $k => $v) {
$o .= "$k=" . urlencode($v) . "&";
}
$post_data = substr($o, 0, -1);
} else {
$post_data = json_encode($post_data);
}
$postUrl = IP地址/端口号/$name;
$curlPost = $post_data;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_TIMEOUT, 5); //只需要设置一个秒的数量就可以
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
$info = curl_getinfo($ch);
file_put_contents('kk.txt', json_encode($info) . "\n", FILE_APPEND);
curl_close($ch);
return $info['http_code'];
}
调用方法:pr_post_curl('命令id',$array=array());