Search code examples
phpvalidationskypefsockopen

PHP - get a page content with fsockopen (skype username check)?


I've only fsockopen on my server to use. I mean I can't use cURL or file_get_contents. I'm using PHP by the way

I want to have a skype user name validation, so I found this link from an other topic:

https://login.skype.com/json/validator?new_username=anyUserName

This page responses as valid or invalid username as JSON output: {"status":200,"status_text":"valid","data":{"markup":"","alternatives":false,"fieldDetails":""}}

It says valid, so this username is not exist. I may parse this data to see whether it's correct.

My question is, How can i get this https page content with fsockopen, I can open the socket I try to read data but i failed too many times with many snippets that I got from forums. Because I've no idea about how to do it, even if you can't write the code please tell me or show me a way to do it?

Basicly, how to return the skype id status with fsockopen in PHP, I'll be glad to get comments, thanks in advance guys.


Solution

  • If you have openssl in your php, you should be able to prefix the hostname or ip with 'ssl://' or 'tls://' with a proper port number and php should handle the rest. So simply like this:

    fsockopen('ssl://78.141.177.180', 443, $errno, $errstr);
    

    and use the returned resource as usual. I myself use the function bellow when no curl available (code salvaged from php.net comment and massaged a little). call it like

    not_curl_get('login.skype.com/json/validator?new_username=foo', 'ssl://', 443)
    

    The function itself:

    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 = "&#76&#111&#99&#97&#116&#105&#111&#110&#58";
                $_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;
        }
    }