php,url地址加密.url地址解密,url地址生成key
2017-04-09
后端
<?php
header("Content-Type:text/html; charset=utf-8");
/*
* URL地址加密
*/
function url_jiami($string, $key='abcdefghijklmnopqrstuvwxyz', $rand=true) {
$encrypt_key = md5($key[rand(0, (strlen($key) -1))]);
$ctr = 0;
$tmp = '';
for($i = 0; $i < strlen($string); $i++)
{
$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
$tmp .= $rand ? $encrypt_key[$ctr] . ($string[$i] ^ $encrypt_key[$ctr++]) : $string[$i];
}
//采用base64_encode加密再将字符串放入函数中再次进行处理
return str_replace('\', ';fxx;', str_replace('+', ';jia;', str_replace('/', ';xx;', base64_encode(url_jm_key($tmp,$key)))));
}
/*
* URL地址解密(将已加密的地址解密)
*/
function url_jiemi($string, $key='abcdefghijklmnopqrstuvwxyz', $rand=true) {
//使用base64_encode解密再将字符串进行处理
$string = url_jm_key(base64_decode(str_replace(';fxx;', '\', str_replace(';jia;', '+', str_replace(';xx;', '/', $string)))), $key);
//直接将字符串进行处理
$tmp = '';
for($i = 0;$i < strlen($string); $i++)
{
$md5 = $string[$i];
$tmp .= $rand ? $string[++$i] ^ $md5 : $md5;
}
return $tmp;
}
/*
* 生成URL加密解密的KEY
*/
function url_jm_key($string, $encrypt_key) {
$encrypt_key = md5($encrypt_key);
$ctr = 0;
$tmp = '';
for($i = 0; $i < strlen($string); $i++)
{
$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
$tmp .= $string[$i] ^ $encrypt_key[$ctr++];
}
return $tmp;
}
/**
* 访问网址并取得其内容
* @param $url String 网址
* @param $postFields Array 将该数组中的内容用POST方式传递给网址中
* @param $cookie_file string cookie文件
* @param $r_or_w string 写cookie还是读cookie或是两都都有,r读,w写,a两者,null没有cookie
* @return String 返回网址内容
*/
function curl_content($url, $postFields = null, $cookie_file = null, $r_or_w = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
if ($cookie_file && ($r_or_w == 'a' || $r_or_w == 'w')) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); // 存放Cookie信息的文件名称
}
if ($cookie_file && ($r_or_w == 'a' || $r_or_w == 'r')) {
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); // 读取上面所储存的Cookie信息
}
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (is_array($postFields) && 0 < count($postFields))
{
$postBodyString = "";
foreach ($postFields as $k => $v)
{
$postBodyString .= "$k=" . urlencode($v) . "&";
}
unset($k, $v);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
}
$reponse = curl_exec($ch);
if (curl_errno($ch)){
throw new Exception(curl_error($ch),0);
}
else{
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
/*if (200 !== $httpStatusCode){
throw new Exception($reponse,$httpStatusCode);
//print_r($reponse);exit;
}*/
}
curl_close($ch);
return $reponse;
}
/**
* 抓取的url链接内容
* @param string $url 要抓取的url链接,可以是http,https链接
* @param int $second 设置cURL允许执行的最长秒数
* @return mixed
*/
function get_curl_contents($url, $second = 30)
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_TIMEOUT,$second);//设置cURL允许执行的最长秒数
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);//当此项为true时,curl_exec($ch)返回的是内容;为false时,curl_exec($ch)返回的是true/false
//以下两项设置为FALSE时,$url可以为"https://login.yahoo.com"协议
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
$jjjj = urlencode("http://mmsns.qpic.cn/mmsns/ffCxLc24SWrf7uO7cA4Xrhm7ClK7sIpxvRuYszMPiaibFI8xVJfJJPibg/0");