如果您的 php 中有 openssl,您应该能够在主机名或 ip 前面加上'ssl://'
或'tls://'
使用正确的端口号,而 php 应该处理其余部分。所以就像这样:
fsockopen('ssl://78.141.177.180', 443, $errno, $errstr);
并像往常一样使用返回的资源。当没有可用的 curl 时,我自己使用下面的函数(代码从php.net 评论中抢救并稍作按摩)。称之为
not_curl_get('login.skype.com/json/validator?new_username=foo', 'ssl://', 443)
函数本身:
function not_curl_get($url, $protocol = '', $port = 80, $options = array()) {
$options = array_merge(array(
'follow_location' => true,
'return_response_part' => 'body',
'referer' => false,
'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; hu; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)',
'cookie_file' => false,
'request_type' => 'get',
), $options);
global $_NOT_CURL_GET_OLDHEADER;
$url = str_replace("http://","",$url);
if (preg_match("#/#","$url")){
$page = $url;
$url = @explode("/",$url);
$url = $url[0];
$page = str_replace($url,"",$page);
if (!$page || $page == ""){
$page = "/";
}
$ip = gethostbyname($url);
}else{
$ip = gethostbyname($url);
$page = "/";
}
$open = fsockopen($protocol.$ip, $port, $errno, $errstr, 60);
if ($options['request_type'] === 'post'){
$send = "POST $page HTTP/1.0\r\n";
}else{
$send = "GET $page HTTP/1.0\r\n";
}
$send .= "Host: $url\r\n";
if ($options['referer']){
$send .= "Referer: ".$options['referer']."\r\n";
}
if ($options['cookie_file']){
if (@file_exists($options['cookie_file'])){
$cookie = urldecode(@file_get_contents($options['cookie_file']));
if ($cookie){
$send .= "Cookie: $cookie\r\n";
$add = @fopen($options['cookie_file'],'w');
fwrite($add,"");
fclose($add);
}
}
}
$send .= "Accept-Language: en-us, en;q=0.50\r\n";
if ($options['user_agent']){
$send .= "User-Agent: ".$options['user_agent']."\r\n";
}
if ($options['request_type']){
$send .= "Content-Type: application/x-www-form-urlencoded\r\n";
$send .= "Content-Length: " .strlen($options['request_type']) ."\r\n\r\n";
$send .= $options['request_type'];
}else{
$send .= "Connection: Close\r\n\r\n";
}
fputs($open, $send);
$return = '';
while (!feof($open)) {
$return .= fgets($open, 4096);
}
fclose($open);
$return = @explode("\r\n\r\n",$return,2);
$header = $return[0];
if ($options['cookie_file']){
if (preg_match("/Set\-Cookie\: /i","$header")){
$cookie = @explode("Set-Cookie: ",$header,2);
$cookie = $cookie[1];
$cookie = explode("\r",$cookie);
$cookie = $cookie[0];
$cookie = str_replace("path=/","",$cookie[0]);
$add = @fopen($options['cookie_file'],'a');
fwrite($add,$cookie,strlen($read));
fclose($add);
}
}
if ($_NOT_CURL_GET_OLDHEADER){
$header = "$_NOT_CURL_GET_OLDHEADER<br /><br />\n$header";
}
if ($return[1]){
$body = $return[1];
}else{
$body = "";
}
if ($options['return_response_part'] === 'body'){
$return = $body;
}
if ($options['return_response_part'] === 'head'){
$return = $header;
}
if ($options['return_response_part'] === 'all'){
$return = "$header$body";
}
if ($options['follow_location']){
if (preg_match("/Location\:/","$header")){
$url = @explode("Location: ",$header);
$url = $url[1];
$url = @explode("\r",$url);
$url = $url[0];
$_NOT_CURL_GET_OLDHEADER = str_replace("\r\n\r\n","",$header);
$l = "Location:";
$_NOT_CURL_GET_OLDHEADER = str_replace("Location:",$l,$_NOT_CURL_GET_OLDHEADER);
return not_curl_get($url, $protocol, $port, $options);
}else{
return $return;
}
}else{
return $return;
}
}