During upload content for my portal under MPS project, i always facing repetitive task that make me wasting time.
The scenario is i need to download all PDF files and upload it to the new server. Then i need to obtain each new PDF file's link to replace old link from content template.
The Overview is:
Template <-- consist old PDF links <--- Replace Old Links With the new one
Here my code:
<?php
set_time_limit(0);
//... some code for getting post data here
function grab($url,$cookie)
{
$curl_connection = curl_init($url);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 40);
curl_setopt($curl_connection, CURLOPT_TIMEOUT, 120);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_HEADER, true);
curl_setopt($curl_connection, CURLINFO_HEADER_OUT, true);
curl_setopt($curl_connection, CURLOPT_COOKIE,$cookie);
$result = curl_exec($curl_connection);
$info = curl_getinfo($curl_connection);
//echo $info["request_header"];
curl_close($curl_connection);
file_put_contents('d:/list.txt',$result);
return $result;
}
$url ='http://xxx.xxx.xxx.xxx:2/group/control_panel/view/64951';
$cookie = 'JSESSIONID=F31547DD37DFF3F4CA84713725F670D4; LOGIN=7973636f6e74656e743140736b616c692e6e6574; REMEMBER_ME=true';
$result = grab($url,$cookie);
//how about if there are pages, get total pages
$pat = "#&cur[\d]+=([\d]+)#is";
preg_match_all($pat, $result, $match);
$pages = array_unique($match[0]);
$totpages = explode("=",end($pages));
$totpage = $totpages[1];
//lets grab
$previmgs = array();
$pages = array();
$imgs = array();
for($i=0;$i<$totpage;$i++)
{
$page = $url.'?'.$totpages[0].'='.($i+1);
$pages[] = $page;
$result = grab($page ,$cookie);
$pat = "#http:\/\/xxx.xxx.xxx.xxx:2\/group\/control_panel\/manage\/\-\/document_library\/view\/[\d]+\/[\d]+#is";
preg_match_all($pat, $result, $match);
$links = array_values(array_unique($match[0]));
$j=0;
foreach($links as $link)
{
$result = grab($link ,$cookie);
$pat = "#<input[^>]+class=\"form-text[^>]+value=\".*?(&\#x2f;documents.*?)\"#is";
preg_match($pat, $result, $m);
array_push($imgs, 'http://xxx.xxx.xxx.xxx:2'.html_entity_decode($m[1]));
array_push($previmgs, $link);
//if($j>3) break;
//$j++;
}
}
$links = array_values(array_unique($previmgs));
echo '<pre>';
print_r($pages);
print_r($links);
print_r($imgs);
$template = file_get_contents('d:/template.txt');
foreach($imgs as $img) {
$template = preg_replace('~\[LINK\]~',$img,$template,1);
}
file_put_contents('d:/done.txt',$template);
?>
Hope it help others..
Wahuallam.
No comments:
Post a Comment