用curl post 调用接口
- 业务代码省略
代码调用部分
// +- curl post 调mars接口
$url = '192.168.1.174/mars/surface.php?s=Interface/openInterface';
$post_data['weight'] = $orders_['deal_num'];
$post_data['dealer_name'] = $order_['dealer_name'];
$post_data['id'] = $orders_['id'];
$post_data['type'] = $goods_['oil_type'];
$o = "";
foreach ( $post_data as $k => $v ){
// 拼装成想要的URL格式
$o.= "$k/" . urlencode( $v ). "/" ;
}
$post_data = substr($o,0,-1);
// 调用request_post
$res = request_post($url, $post_data);
request_post 方法
/**
* 模拟post进行url请求
* @param string $url
* @param string $param
*/
function request_post($url = '', $param = '') {
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch);
return $data;
}
@摘录自牛逼的霍啸林的博客
@链接地址:http://www.cnblogs.com/jiqing9006/p/3949190.html