我的服务器上只有fsockopen可供使用。我的意思是我不能使用cURL或file_get_contents。我使用PHP顺便说一句
我想进行skype用户名验证,所以我从另一个主题找到了这个链接:
https://login.skype.com/json/validator?new_username=anyUserName
此页面将有效或无效的用户名作为JSON输出进行响应:{"status":200,"status_text":"valid","data":{"markup":","alternates":false,"fieldDetails":"}}
上面写着有效,所以这个用户名不存在。我可能会分析这些数据,看看它是否正确。
我的问题是,我如何使用fsockopen获取此https页面内容,我可以打开套接字,我试图读取数据,但我失败了太多次,因为我从论坛中获得了很多片段。因为我不知道该怎么做,即使你不会写代码,也请告诉我或给我一个方法?
基本上,如何在PHP中使用fsockopen返回skype id状态,我很高兴收到评论,提前感谢各位。
如果你的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;
}
}