我正在尝试从网页获取图像并将其保存到我的PC,但是当我通过抓住表单页面并将其保存在变量中来传递其URL时。并将该变量传递给我的curl图片获取函数,它将返回404。 但是,当我打印捕获的url变量并通过复制粘贴将变量的打印值传递给函数时,它将返回200(确定)并保存图像。
这是我的代码:
preg_match('/id=\"searchForm:j_idt40\" src=\"(.*?)\"/', open('http://pas.fdle.state.fl.us/pas/restricted/PAS/StolenBoats.jsf'), $matches);
$url2= "http://pas.fdle.state.fl.us".$matches[1];
echo $url2;
grab_image($url2,"image2.jpg");
function open($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIE, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "1"); //writes cookie
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_REFERER, 'http://pas.fdle.state.fl.us/pas/restricted/PAS/StolenBoats.jsf');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function get_web_page( $url )
{
$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$options = array(
CURLOPT_CUSTOMREQUEST =>"GET", //set request type post or get
CURLOPT_POST =>false, //set to GET
CURLOPT_USERAGENT => $user_agent, //set user agent
CURLOPT_COOKIEFILE =>"cookie.txt", //set cookie file
CURLOPT_COOKIEJAR =>"cookie.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
$code=curl_getinfo($ch,CURLINFO_HTTP_CODE);
echo"<br>resp code:".$code;
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
function grab_image($url1,$save) {
echo"<br>#########################################################<br>";
echo"URL=".$url1;
$result = get_web_page($url1);
echo"<br>Errorno:".$result['errno'];
echo"<br>Error msg:".$result['errmsg'];
echo"<br>Content:".$result['content'];
if(file_exists($save)){
unlink($save);
}
$fp = fopen($save,'x');
fwrite($fp, $result['content']);
fclose($fp);
}
输出为:
http://pas.fdle.state.fl.us/pas/javax.faces.resource/dynamiccontent.properties.jsf;jsessionid=yJ3Orpy4CG94G60BI1kpm43CSmuV8TYUOFa3IRr_.MEGADETHPRIMARY?ln=primefaces&v=6.1.2&pfdrid=558b6b95c559b790d2a10e03dbb80110&pfdrt=sc&pfdrid_c=false&uid=431a2d2a-970e-4584-9c6b-c7f88fb30b38
#########################################################
URL=http://pas.fdle.state.fl.us/pas/javax.faces.resource/dynamiccontent.properties.jsf;jsessionid=yJ3Orpy4CG94G60BI1kpm43CSmuV8TYUOFa3IRr_.MEGADETHPRIMARY?ln=primefaces&v=6.1.2&pfdrid=558b6b95c559b790d2a10e03dbb80110&pfdrt=sc&pfdrid_c=false&uid=431a2d2a-970e-4584-9c6b-c7f88fb30b38
resp code:404
Errorno:0
Error msg:
Content:
#########################################################
var_dump($ url)结果为:
dump before calling function:
string(297) "http://pas.fdle.state.fl.us/pas/javax.faces.resource/dynamiccontent.properties.jsf;jsessionid=QoecD0pG5uUUzXKOtMalTPJQhVu8ZkZFN7Y2VWYo.MEGADETHPRIMARY?ln=primefaces&v=6.1.2&pfdrid=558b6b95c559b790d2a10e03dbb80110&pfdrt=sc&pfdrid_c=false&uid=f15b2a89-ae8e-49d5-ac70-933c026b1ecb"
dump before curl execution inside function
string(297) "http://pas.fdle.state.fl.us/pas/javax.faces.resource/dynamiccontent.properties.jsf;jsessionid=QoecD0pG5uUUzXKOtMalTPJQhVu8ZkZFN7Y2VWYo.MEGADETHPRIMARY?ln=primefaces&v=6.1.2&pfdrid=558b6b95c559b790d2a10e03dbb80110&pfdrt=sc&pfdrid_c=false&uid=f15b2a89-ae8e-49d5-ac70-933c026b1ecb"
resp code:404
dump after curl:
string(297) "http://pas.fdle.state.fl.us/pas/javax.faces.resource/dynamiccontent.properties.jsf;jsessionid=QoecD0pG5uUUzXKOtMalTPJQhVu8ZkZFN7Y2VWYo.MEGADETHPRIMARY?ln=primefaces&v=6.1.2&pfdrid=558b6b95c559b790d2a10e03dbb80110&pfdrt=sc&pfdrid_c=false&uid=f15b2a89-ae8e-49d5-ac70-933c026b1ecb"