標籤:
支援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 庫使用