php curlfile无效,php – 从CURL请求和file_get_contents收到的无效cookie

我上面的评论中有几个未解答的问题,但我会分享这些代码用于示例目的.这是我过去用作使用cURL的浏览器仿真基类的东西:

if(!function_exists("curl_init")) { throw new Exception("CurlBrowser requires the cURL extension, which is not enabled!"); }

class CurlBrowser

{

public $userAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0";

/*

Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0

Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");

Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0

*/

public $cookiesFile = null;

public $proxyURL = null;

public $saveLastOutput = "";

public $caBundle = "cacert.pem";

public $httpHeaders = array();

public function __construct($UseCookies = true)

{

if(is_bool($UseCookies) && $UseCookies)

{

$this->cookiesFile = dirname(__FILE__)."/cookies.txt";

}

elseif(is_string($UseCookies) && ($UseCookies != ""))

{

$this->cookiesFile = $UseCookies;

}

}

public function SetCustomHTTPHeaders($arrHeaders)

{

$this->httpHeaders = $arrHeaders;

}

public function SetProxy($proxy)

{

$this->proxyURL = $proxy;

}

public function Get($url)

{

return $this->_request($url);

}

public function Post($url,$data = array())

{

return $this->_request($url,$data);

}

private function _request($form_url,$data = null)

{

$ch = curl_init($form_url);

// CA bundle

$caBundle = $this->caBundle;

if(file_exists($caBundle))

{

// Detect and convert relative path to absolute path

if(basename($caBundle) == $caBundle)

{

$caBundle = getcwd() . DIRECTORY_SEPARATOR . $caBundle;

}

// Set CA bundle

curl_setopt($ch, CURLOPT_CAINFO, $caBundle);

}

// Cookies

if($this->cookiesFile !== null)

{

curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiesFile);

curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiesFile);

}

// User Agent

curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);

// Misc

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_ENCODING, "gzip, deflate");

// Optional proxy

if($this->proxyURL !== null)

{

curl_setopt($ch, CURLOPT_PROXY, $this->proxyURL);

}

// Custom HTTP headers

if(count($this->httpHeaders))

{

curl_setopt($ch, CURLOPT_HTTPHEADER, $this->httpHeaders);

}

// POST data

if($data !== null)

{

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

}

// Run operation

$result = curl_exec($ch);

if($result === false)

{

throw new Exception(curl_error($ch));

}

else

{

if(!empty($this->saveLastOutput))

{

file_put_contents($this->saveLastOutput,$result);

}

return $result;

}

}

}

?>

你会这样使用它:

$browser = new CurlBrowser();

$html = $browser->Get("https://....");

...etc...

我猜测你只是在原始代码中错过了一个cookie jar,但这主要是基于直觉,因为我们目前还没有你所有的问题代码.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值