一curl的使用例子

来源:互联网 发布:网页描述如何优化 编辑:程序博客网 时间:2024/05/21 04:20

$content =  array('user_id'=>123123,'goods_name'=>'商品名称test','order_id'=>"20161111232");
$content = json_encode($content);


$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";

$url = 'http://www.网址';
$header = array ('Content-type: application/x-www-form-urlencoded');
$curl = curl_init (); // 启动一个CURL会话
$data = array('content'=>$content);

if(is_array($data)){
    $data = http_build_query($data);  

}

/*$data = array('foo'=>'bar',
              'baz'=>'boom',
              'cow'=>'milk',
              'php'=>'hypertext processor');
echo http_build_query($data);
 输出:
       foo=bar&baz=boom&cow=milk&php=hypertext+processor  */

curl_setopt($curl, CURLOPT_HTTPHEADER, $header);  //设置头信息的地方
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); // 模拟用户使用的浏览器
@curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 120); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
    echo curl_error ( $curl );

    return false;
}

curl_close($curl); // 关闭CURL会话



接收端:$content = $_POST['content'];
$data = json_decode($content,true);

0 0
原创粉丝点击