php curl 庫使用

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.