gpt4 book ai didi

php - 保存图像仅在登录时可用

转载 作者:行者123 更新时间:2023-12-04 20:33:55 24 4
gpt4 key购买 nike

当我登录到一个要求您登录的网站时,我在下载图片时遇到了一些问题。图片只能在您登录该网站时查看,但您似乎无法直接查看它们在浏览器中,如果您将其位置复制到选项卡/新窗口中(它会重定向到错误页面 - 所以我猜包含的文件夹已被 .htaccess-ed)。

无论如何,我下面的代码允许我登录并获取 HTML 内容,效果很好 - 但我无法获取图像......这就是我需要帮助的地方!

<?
// curl.php

class Curl {

public $cookieJar = "";

public function __construct($cookieJarFile = 'cookies.txt') {
$this->cookieJar = $cookieJarFile;
}

function setup() {
$header = array();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/gif;q=0.8,image/x-bitmap;q=0.8,image/jpeg;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // browsers keep this blank.

curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7');
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookieJar);
curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookieJar);
curl_setopt($this->curl, CURLOPT_AUTOREFERER, true);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
}

function get($url) {
$this->curl = curl_init($url);
$this->setup();

return $this->request();
}

function getAll($reg, $str) {
preg_match_all($reg, $str, $matches);
return $matches[1];
}

function postForm($url, $fields, $referer = '') {
$this->curl = curl_init($url);
$this->setup();
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_REFERER, $referer);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields);
return $this->request();
}

function getInfo($info) {
$info = ($info == 'lasturl') ? curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL) : curl_getinfo($this->curl, $info);
return $info;
}

function request() {
return curl_exec($this->curl);
}
}

?>

下面是使用它的页面。

<?
// data.php

include('curl.php');
$curl = new Curl();

$url = "http://domain.com/login.php";
$newURL = "http://domain.com/go_here.php";

$username = "user";
$password = "pass";

$fields = "user=$username&pass=$password";

// Calling URL
$referer = "http://domain.com/refering_page.php";

$html = $curl->postForm($url, $fields, $referer);

$html = $curl->get($newURL);
echo $html;

?>

我已经尝试将图像的直接 URL 放入 $newURL 中,但是没有得到图像 - 它只是返回一个错误,说因为该文件夹不可直接查看。我已经尝试以不同的方式改变上述内容,但我没有成功获得图像,尽管我已经设法通过基本上说错误 405 和/或 406(但不是我想要的图像)来获得屏幕。

任何帮助都会很棒!

最佳答案

哇,

似乎是一个令人费解的问题。

我要做的是将浏览器 session 与 HTTP 层的 PHP 代码进行比较,看看有什么不同。

捕获Wireshark , 使用您的浏览器成功连接。您将需要过滤掉所有其他流量,只转储端口 80 上的流量。如果您右键单击数据包并单击“跟随 TCP 流”,它将为您提供 HTTP header 和页面输出。

然后执行相同的操作,但这次使用 PHP 脚本。

然后比较 header ,看看有什么不同。也许您缺少一两个 header ,也许您需要先转到某个页面,也许您的 PHP 脚本没有发送正确的 cookie。

关于php - 保存图像仅在登录时可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3011794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com