首页 > Web开发 > 详细

php curl 库使用

时间:2016-08-24 12:36:19      阅读:226      评论:0      收藏:0      [点我收藏+]

支持http basic认证:

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "admin:admin");

去除http报头

if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == ‘200‘) {
    $header_size    = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $headers        = substr($response, 0, $header_size);
    $body        = substr($response, $header_size);
}

完整案例:

$ch = curl_init(‘http://172.16.100.106/cur_run_stat.xml‘); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "admin:admin");
// curl_setopt($ch,CURLOPT_PROXY,‘127.0.0.1:8888‘);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);//ÈôPHP±àÒëʱ²»´øopensslÔòÐèÒª´ËÐÐ 

// curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: text/xml‘)); 

    // curl_setopt($ch, CURLOPT_HEADER, true);    //表示需要response header
    // curl_setopt($ch, CURLOPT_NOBODY, false); //表示需要response body

// curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
// curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//POST数据

// curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data"); 
// 3. Ö´Ðв¢»ñÈ¡HTMLÎĵµÄÚÈÝ
$response = curl_exec($ch);



if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == ‘200‘) {
    $header_size    = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $headers        = substr($response, 0, $header_size);
    $body        = substr($response, $header_size);
}

curl_close($ch);
echo $body;

file_get_contents进行http basic认证

$user = "admin";
$passwd = "admin";
$opts = array(
  ‘http‘=>array(
        ‘method‘=>"GET",
        ‘timeout‘=>5,
        // "Authorization"=>"Basic YWRtaW46YWRtaW4="
        ‘header‘ => "content-type:application/x-www-form-urlencoded\r\nAuthorization:Basic ".base64_encode($user.":".$passwd)."=\r\n"// 把$auth加入到header  
   )
);


$context = stream_context_create($opts);

$url = "http://172.16.100.106/cur_run_stat.xml";
   

         $context =  file_get_contents($url,false,$context);
         echo $context;

 

php curl 库使用

原文:http://www.cnblogs.com/hzijone/p/5802428.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!