使用cURL传递cookie [英] Passing cookie using cURL

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

问题描述

我意识到这已经被覆盖了很多次,但由于某种原因,它仍然不为我工作。我尝试通过cURL将Cookie传递到某个网页,但目标网页仍然无法接收。

I realize that this has been covered a bunch of times but for some reason it's still not working for me. I'm trying to pass a cookie to a page via cURL but the destination page still won't pick it up.

以下相关代码。

        $cookie = "did=1";
        $ch = curl_init();
    $cj=tempnam("/","cookiejar");
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1;       en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13')" );
        curl_setopt( $ch, CURLOPT_COOKIE, $cookie );
        curl_setopt( $ch, CURLOPT_ENCODING, "" );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_AUTOREFERER, true );

谢谢。

推荐答案

尝试使用

$ckfile = tempnam ("/", "CURLCOOKIE"); 

这里有示例的方法。

<?php
$ckfile = tempnam ("/", "CURLCOOKIE");

$ch = curl_init ("http://somedomain.com/");
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);

$ch = curl_init ("http://somedomain.com/cookiepage.php");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);

/* here you can do whatever you want with $output */
?>

EDIT

class CURL {
var $callback = false;

function setCallback($func_name) {
$this->callback = $func_name;
}

function doRequest($method, $url, $vars) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
if ($method == 'POST') {
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
$data = curl_exec($ch);
curl_close($ch);
if ($data) {
    if ($this->callback)
    {
        $callback = $this->callback;
        $this->callback = false;
        return call_user_func($callback, $data);
    } else {
        return $data;
    }
} else {
    return curl_error($ch);
}
}

function get($url) {
return $this->doRequest('GET', $url, 'NULL');
}

function post($url, $vars) {
return $this->doRequest('POST', $url, $vars);
}
}
?>

此处

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

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