码迷,mamicode.com
首页 > Web开发 > 详细

php curl 库使用

时间:2016-08-24 12:39:14      阅读:152      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!