web服务器系统 Ubuntu 12.04
安装curl库
查找php5的curl支持库
apt-cache search curl | grep php
得到 php5-curl - CURL module for php5
apt-get install curl php5-curl
<?php header("Content-Type: text/html; charset=utf-8"); $host = '10.0.1.191'; //主机 $path = '/itmng/cgi-bin/showlog.cgi?jsonoutput'; //路径 $user = 'smt'; //用户名 $pass = '123'; //密码 //使用curl $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://{$host}{$path}"); curl_setopt($ch, CURLOPT_PORT, 80); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_HEADER, 0); //设置头文件是否输出 //curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_USERPWD, "{$user}:{$pass}"); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); //FALSE 不输出内容 $return = curl_exec($ch); if (!$return) { echo curl_error($ch); } else { echo $return; } curl_close($ch); ?>
打开/etc/php5/cli/php.ini 中的allow_url_fopen选项,设置为“ On”
<?php $host = '10.0.1.191'; //主机 $path = '/itmng/cgi-bin/showlog.cgi?jsonoutput'; //路径 $user = 'smt'; //用户名 $pass = '123'; //密码 //$queryString = '?a=1&b=2'; //query字串 //使用fsockopen $authorizationToken = base64_encode("{$user}:{$pass}"); //$fp = fsockopen('tcp://' . $host, 80, $errorNo, $errorMsg, 45); $fp = fsockopen($host,80, $errorNo, $errorMsg, 3); if (!$fp) { echo 'error: ', $errorNo, ' ', $errorMsg; } else { //$header = "GET {$path}{$queryString} HTTP/1.1\r\n"; $header = "GET {$path} HTTP/1.1\r\n"; $header .= "Host: {$host}\r\n"; //$header .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n"; $header .= "Authorization: Basic {$authorizationToken}\r\n"; //认证 $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; // $header .= "Content-length: " . strlen($queryString) . "\r\n"; $header .= "Connection: Close\r\n\r\n"; fwrite($fp, $header); $return = ''; while (!feof($fp)) { $return .= fgets($fp); } //去掉头文件 if ($return) { $body = stristr($return, "\r\n\r\n"); $body =substr($body, 4, strlen($body)); $return = $body; } fclose($fp); echo $return; } ?>