PHP CURL。有人可以帮我这个吗?

时间:2016-09-09 05:02:34

标签: php curl php-curl

我目前遇到了问题。这是我登录特定网站的编码卷曲。我希望有人在那里检查我的问题究竟发生了什么:)

这是我的函数代码:

    class curl
    {
        private $options;
        public $errno;
        public $errmsg;
        public $content;
        public $headers;
        public $httpcode;
    public function __construct()
    {
        global $config;
        $this->options = array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_SSL_VERIFYHOST => 2,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_AUTOREFERER => true,
            CURLOPT_COOKIEFILE => $config['cookie_file'],
            CURLOPT_COOKIEJAR => $config['cookie_file']);
    }

    public function setopt($key, $val)
    {
        eval('$opt = CURLOPT_' . strtoupper($key) . ';');
        $this->options[$opt] = $val;
    }

    public function removeopt($key)
    {
        switch (strtolower($key))
        {
            case 'post':
                unset($this->options[CURLOPT_POST]);
                unset($this->options[CURLOPT_POSTFIELDS]);
                break;
            case 'sock5':
                unset($this->options[CURLOPT_HTTPPROXYTUNNEL]);
                unset($this->options[CURLOPT_PROXY]);
                unset($this->options[CURLOPT_PROXYTYPE]);
                break;
            default:
                eval('$opt = CURLOPT_' . strtoupper($key) . ';');
                unset($this->options[$opt]);
        }
    }

    public function sock5($sock)
    {
        $this->options[CURLOPT_HTTPPROXYTUNNEL] = true;
        $this->options[CURLOPT_PROXY] = $sock;
        $this->options[CURLOPT_PROXYTYPE] = CURLPROXY_SOCKS5;
    }

    public function ref($ref)
    {
        $this->options[CURLOPT_REFERER] = $ref;
    }

    public function httpheader($headers)
    {
        $this->options[CURLOPT_HTTPHEADER] = $headers;
    }

    public function cookies($cookies)
    {
        $this->options[CURLOPT_COOKIE] = $cookies;
        $this->removeopt('cookiefile');
        $this->removeopt('cookiejar');
    }

    public function header($val = false)
    {
        $this->options[CURLOPT_HEADER] = $val;
    }

    public function nobody($val = false)
    {
        $this->options[CURLOPT_NOBODY] = $val;
    }

    public function ua($ua)
    {
        $this->options[CURLOPT_USERAGENT] = $ua;
    }

    public function postdata($postdata)
    {
        if (is_array($postdata))
        {
            $post_array = array();
            foreach ($postdata as $key => $value)
            {
                $post_array[] = urlencode($key) . '=' . urlencode($value);
            }
            $post_string = implode('&', $post_array);
        } else
        {
            $post_string = $postdata;
        }
        $this->options[CURLOPT_POST] = true;
        $this->options[CURLOPT_POSTFIELDS] = $post_string;
    }

    public function page($url)
    {
        $ch = curl_init($url);
        curl_setopt_array($ch, $this->options);
        $this->content = curl_exec($ch);
        $this->errno = curl_errno($ch);
        $this->errmsg = curl_error($ch);
        $this->headers = curl_getinfo($ch);
        $this->httpcode = $this->headers['http_code'];
        curl_close($ch);
        $this->removeopt('post');
    }

    public function validate()
    {
        if ($this->errno)
        {
            return false;
        }
        return true;
    }

    public function display($d = 0)
    {
        echo $this->content;
        if ($d)
        {
            exit;
        }
    }

    public function getStr($find_start = '', $find_end = '')
    {
        if ($find_start == '')
        {
            return '';
        }
        $start = strpos($this->content, $find_start);
        if ($start === false)
        {
            return '';
        }
        $length = strlen($find_start);
        $substr = substr($this->content, $start + $length);
        if ($find_end == '')
        {
            return $substr;
        }
        $end = strpos($substr, $find_end);
        if ($end === false)
        {
            return $substr;
        }
        return substr($substr, 0, $end);
    }

    public function has($str, $type = 1)
    {
        $bool = $type == 1 ? stripos($this->content, $str) : strpos($this->content, $str);
        return $bool !== false;
    }
}

set_time_limit(0);
$dir = dirname(__file__);
$config['cookie_file'] = $dir . '/cookies/' . md5($_SERVER['REMOTE_ADDR']) .
    'ebay.cookie';

if (!file_exists($dir . '/cookies/index.html'))
{
    $f = fopen($dir . '/cookies/index.html', 'w');
    fwrite($f, 'hhhhhhhhhhhhh');
    fclose($f);
}

if (!file_exists($config['cookie_file']))
{
    delete_cookies();
}

function delete_cookies()
{
    global $config;
    $f = fopen($config['cookie_file'], 'w');
    fwrite($f, '');
    fclose($f);
}

function get($list)
{
    preg_match_all("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5}/", $list, $socks);
    return $socks[0];
}

function xflush()
{
    static $output_handler = null;
    if ($output_handler === null)
    {
        $output_handler = @ini_get('output_handler');
    }
    if ($output_handler == 'ob_gzhandler')
    {
        return;
    }
    flush();
    if (function_exists('ob_flush') and function_exists('ob_get_length') and
        ob_get_length() !== false)
    {
        @ob_flush();
    } else
        if (function_exists('ob_end_flush') and function_exists('ob_start') and
            function_exists('ob_get_length') and ob_get_length() !== false)
        {
            @ob_end_flush();
            @ob_start();
        }
}
function getCookies($str){
    preg_match_all('/Set-Cookie: ([^; ]+)(;| )/si', $str, $matches);
    $cookies = implode(";", $matches[1]);
    return $cookies;
}
function fetch_value($str, $find_start = '', $find_end = '')
{
    if ($find_start == '')
    {
        return '';
    }
    $start = strpos($str, $find_start);
    if ($start === false)
    {
        return '';
    }
    $length = strlen($find_start);
    $substr = substr($str, $start + $length);
    if ($find_end == '')
    {
        return $substr;
    }
    $end = strpos($substr, $find_end);
    if ($end === false)
    {
        return $substr;
    }
    return substr($substr, 0, $end);
}

function array_remove_empty($arr)
{
    $narr = array();
    while (list($key, $val) = each($arr))
    {
        if (is_array($val))
        {
            $val = array_remove_empty($val);
            // does the result array contain anything?
            if (count($val) != 0)
            {
                // yes :-)
                $narr[$key] = trim($val);
            }
        } else
        {
            if (trim($val) != "")
            {
                $narr[$key] = trim($val);
            }
        }
    }
    unset($arr);
    return $narr;
}

function display($m, $t = 1, $d = 0)
{
    if ($t == 1)
    {
        echo '<div>' . $m . '</div>';
    } else
    {
        echo $m;
    }
    if ($d)
    {
        exit;
    }
}

这是我的卷曲代码:

require ('./functions.php');

    $curl = new curl();
    delete_cookies();
    $curl->ua('Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0');
    $curl->ref('https://www.vodafone.co.uk/myvodafone/faces/home');
    $result = array();
    delete_cookies();
    $curl->header(true);
    $curl->sock5($sock);
    $curl->page('https://www.vodafone.co.uk/myvodafone/faces/home');
    if($curl->validate()){
        $afrLoop = $curl->getStr(', "_afrLoop=','"');
        $curl->page('https://www.vodafone.co.uk/myvodafone/faces/home?_afrLoop='.$afrLoop.'&_afrWindowMode=0&_afrWindowId=null');
        if($curl->validate()){
            $var = array(
                'authn_try_count' => $curl->getStr('authn_try_count" value="','"'),
                'request_id' => $curl->getStr('request_id" value="','"'),
                'OAM_REQ' => urldecode($curl->getStr('OAM_REQ" value="','"')),
                'locale' => $curl->getStr('locale" value="','"'),
            );
            $curl->postdata($var);
            $curl->page('https://www.vodafone.co.uk/myvodafone/faces/oracle/webcenter/portalapp/pagehierarchy/Page1.jspx');
            if($curl->validate()){
                $viewState = $curl->getStr('javax.faces.ViewState" value="','"');
                $oamreq = urldecode($curl->getStr('OAM_REQ" value="','"'));
                $user = urlencode('stacyholmes09@gmail.com');
                password = 'test1234567';
                $curl->postdata('searchQuery=Search&username='.$user.'&password='.$pwd.'&OAM_REQ='.$oamreq.'&org.apache.myfaces.trinidad.faces.FORM=f1&javax.faces.ViewState='.$viewState.'&source=T:oc_5739652421region1:0:loginForm:signin1');
                $curl->page('https://www.vodafone.co.uk/oam/server/auth_cred_submit');
                if($curl->validate()){
                    $error = $curl->getStr('p_error_code" value="','"');
                    if(!$error){
                        if($curl->has('appmanager')){
                            echo 'Working - $user and $password'
                        }else{
                            'Unknown - $user and $password'
                        }
                    }else{
                        'Not Working - $user and $password'
                    }
                }else
                {
                    $result['error'] = 1;
                    $result['msg'] = $sock . ' => Die/Timeout';
                }
            }else
            {
                $result['error'] = 1;
                $result['msg'] = $sock . ' => Die/Timeout';
            }
        }else
        {
            $result['error'] = 1;
            $result['msg'] = $sock . ' => Die/Timeout';
        }
    }else
    {
        $result['error'] = 1;
        $result['msg'] = $sock . ' => Die/Timeout';
    }
    echo json_encode($result);
    exit;

}

?>

问题是每当我执行它时,它总是只是回显未知。我没有看到任何错误。我希望有人可以帮助我:)

那些卷曲的专业人士,请立即查看! :)

1 个答案:

答案 0 :(得分:0)

好奇。你能找到解决方案吗?

我对卷曲不是很好。但是,很多时候,我能够实现file_get_contents函数所需的功能。

这是我修改过的代码片段,我经常使用这段代码:

function getpage($url,$postarray,$authorization = false,$method = "GET") {
    $httpOpts = array("method"  => $method,
                      "header"  => array("Content-type: application/x-www-form-urlencoded"),
                      "content" => $postarray);
    if( $authorization != false ) $httpOpts["header"][] = "Proxy-Authorization: Basic ".base64_encode($authorization);
    if( is_array($postarray)) list($httpOpts["method"],$httpOpts["content"]) = array("POST",http_build_query($postarray));

    $context  = stream_context_create(array("http" => $httpOpts)); // Open stream

    return file_get_contents($url, false, $context);
}

也许这可能会有所帮助......?