php cURL问题 [英] php cURL problems

查看:253
本文介绍了php cURL问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    <?php
$email = "";
$password = "";
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";


//curl get
function curl_get($url, $cookiefile) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

//curl post

function curl_post($url, $cookiefile, $post) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

//cookie file

$cookiefile = "cookie.txt";


//get url to grab GALX & dsh to login
$data = curl_get("https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true&continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&hl=en_US&ltmpl=sso", $cookiefile);


preg_match('/name="GALX"\s*value="(.*?)"/', $data, $galx);

preg_match('/name="dsh" id="dsh"\s*value="(.*?)"/', $data, $dsh);


//login
$data = curl_post("https://www.google.com/accounts/ServiceLoginAuth", $cookiefile, "ltmpl=sso&continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&service=youtube&uilel=3&dsh=$dsh[1]&ltmpl=sso&hl=en_US&ltmpl=sso&timeStmp=&secTok=&GALX=$galx[1]&Email=$email&Passwd=$password&PersistentCookie=yes&rmShown=1&signIn=Sign+in&asts=");

//auth url
$data = curl_get("https://www.google.com/accounts/CheckCookie?continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&hl=en_US&service=youtube&ltmpl=sso&chtml=LoginDoneHtml", $cookiefile);


// youtube 
$data = curl_get("http://www.youtube.com/", $cookiefile);
print $data;

?>

这里是我的代码,在最后一行,当我打印出youtube.com的内容,它显示i没有登录..任何人都知道我在这里做错了吗?登录到谷歌你需要抓住GALX和dsh值,我这样做。这是一个头痛!

here's my code, on the last line when i print out the contents of youtube.com it shows i'm not logged in.. anyone know what i'm doing wrong here? to login to google you need to grab the GALX and dsh values and i'm doing so. this is such a headache!

推荐答案


这是我的代码,在最后一行当我打印youtube.com
的内容显示我尚未登入..

here's my code, on the last line when i print out the contents of youtube.com it shows i'm not logged in..

Google是正确的:) ,你会curl_init()一遍又一遍,这意味着你每次调用curl_get和curl_post函数时启动新会话。您应该只有init一次,并传递您检索的资源。这将解决你的问题。

Google is right :) You see, you curl_init( ) over and over, which means you start new sessions every call to your curl_get and curl_post functions. You should only init once and pass along the resource you retrieve. That should solve your problem.

编辑:自由重写你的代码来说明:

Took the liberty of rewriting your code to illustrate:

<?php
$email = "";
$password = "";



//curl get
function curl_get( $curl, $url, $cookiefile) {
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1" );
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($curl);
    return $data;
}

//curl post

function curl_post( $curl, $url, $cookiefile, $post) {
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1" );
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($curl);
    return $data;
}

//cookie file
$cookiefile = "cookie.txt";

$curl = curl_init( );

//get url to grab GALX & dsh to login
$data = curl_get( $curl, "https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true&continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&hl=en_US&ltmpl=sso", $cookiefile);


preg_match('/name="GALX"\s*value="(.*?)"/', $data, $galx);

preg_match('/name="dsh" id="dsh"\s*value="(.*?)"/', $data, $dsh);


//login
$data = curl_post( $curl, "https://www.google.com/accounts/ServiceLoginAuth", $cookiefile, "ltmpl=sso&continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&service=youtube&uilel=3&dsh=$dsh[1]&ltmpl=sso&hl=en_US&ltmpl=sso&timeStmp=&secTok=&GALX=$galx[1]&Email=$email&Passwd=$password&PersistentCookie=yes&rmShown=1&signIn=Sign+in&asts=");

//auth url
$data = curl_get( $curl, "https://www.google.com/accounts/CheckCookie?continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&hl=en_US&service=youtube&ltmpl=sso&chtml=LoginDoneHtml", $cookiefile);


// youtube 
$data = curl_get( $curl, "http://www.youtube.com/", $cookiefile);
print $data;

这篇关于php cURL问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆