将 goo.gl API 与外部脚本集成
我有一个文件 vars.php 在标头中传递,其中包含:
$link="http://www.mysite.com";
我想使用下面的代码向 mysite.com 附加一个随机的随机字符串,如下 mysite.com/?(random string):
$code = md5(uniqid(rand(), true));
$mywebsite = 'http://mysite.com?kjdf='.$code;
然后将其传递到goo.gl API。这样我每次加载脚本时都会得到一个新的 goo.gl url:
<?php
/*
$googl = new goo_gl('$mywebsite');
echo $googl->result();
*/
class goo_gl{
var $url, $resul;
//goo.gl construct method
function goo_gl($url){
$this->url = $url;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://goo.gl/api/url');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, '[email protected]&url='.urlencode($this->url).'&auth_token='.$this->googlToken($url));
$saida = curl_exec($curl);
curl_close($curl);
if($saida){
$json = json_decode($saida);
$this->resul = $json->short_url;
}
}
//show url shorted by goo.gl
function result(){
return $this->resul;
}
//token code
function googlToken($b){
$i = $this->tke($b);
$i = $i >> 2 & 1073741823;
$i = $i >> 4 & 67108800 | $i & 63;
$i = $i >> 4 & 4193280 | $i & 1023;
$i = $i >> 4 & 245760 | $i & 16383;
$j = "7";
$h = $this->tkf($b);
$k = ($i >> 2 & 15) << 4 | $h & 15;
$k |= ($i >> 6 & 15) << 12 | ($h >> 8 & 15) << 8;
$k |= ($i >> 10 & 15) << 20 | ($h >> 16 & 15) << 16;
$k |= ($i >> 14 & 15) << 28 | ($h >> 24 & 15) << 24;
$j .= $this->tkd($k);
return $j;
}
function tkc(){
$l = 0;
foreach(func_get_args() as $val){
$val &= 4294967295;
$val += $val > 2147483647 ? -4294967296 : ($val < -2147483647 ? 4294967296 : 0);
$l += $val;
$l += $l > 2147483647 ? -4294967296 : ($l < -2147483647 ? 4294967296 : 0);
}
return $l;
}
function tkd($l){
$l = $l > 0 ? $l : $l + 4294967296;
$m = "$l"; //deve ser uma string
$o = 0;
$n = false;
for($p = strlen($m) - 1; $p >= 0; --$p){
$q = $m[$p];
if($n){
$q *= 2;
$o += floor($q / 10) + $q % 10;
} else {
$o += $q;
}
$n = !$n;
}
$m = $o % 10;
$o = 0;
if($m != 0){
$o = 10 - $m;
if(strlen($l) % 2 == 1){
if ($o % 2 == 1){
$o += 9;
}
$o /= 2;
}
}
return "$o$l";
}
function tke($l){
$m = 5381;
for($o = 0; $o < strlen($l); $o++){
$m = $this->tkc($m << 5, $m, ord($l[$o]));
}
return $m;
}
function tkf($l){
$m = 0;
for($o = 0; $o < strlen($l); $o++){
$m = $this->tkc(ord($l[$o]), $m << 6, $m << 16, -$m);
}
return $m;
}
}
?>
结果是每次都不同的 goo.gl 缩短链接。最后我想将其传递到标头(在 php 文件的顶部)。
<?php
$picture="http://www.mysite.com/myimage.gif"; //url to a picture - e.g. http://www.mysite.com/mypic.gif
$link="http://goo.gl/blahblahblah"; //your link.
?>
所以 goo.gl/blahblahblah 会重定向到 mysite.com/?(random_string)
这是一项雄心勃勃的任务,但如果有人可以提供帮助那就太好了
I have a file vars.php passed in the header consisting of:
$link="http://www.mysite.com";
I want to append a random a random string to mysite.com like this mysite.com/?(random string) using the code below:
$code = md5(uniqid(rand(), true));
$mywebsite = 'http://mysite.com?kjdf='.$code;
and then pass this into into the goo.gl api. that way i get a new goo.gl url everytime the script loads:
<?php
/*
$googl = new goo_gl('$mywebsite');
echo $googl->result();
*/
class goo_gl{
var $url, $resul;
//goo.gl construct method
function goo_gl($url){
$this->url = $url;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://goo.gl/api/url');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, '[email protected]&url='.urlencode($this->url).'&auth_token='.$this->googlToken($url));
$saida = curl_exec($curl);
curl_close($curl);
if($saida){
$json = json_decode($saida);
$this->resul = $json->short_url;
}
}
//show url shorted by goo.gl
function result(){
return $this->resul;
}
//token code
function googlToken($b){
$i = $this->tke($b);
$i = $i >> 2 & 1073741823;
$i = $i >> 4 & 67108800 | $i & 63;
$i = $i >> 4 & 4193280 | $i & 1023;
$i = $i >> 4 & 245760 | $i & 16383;
$j = "7";
$h = $this->tkf($b);
$k = ($i >> 2 & 15) << 4 | $h & 15;
$k |= ($i >> 6 & 15) << 12 | ($h >> 8 & 15) << 8;
$k |= ($i >> 10 & 15) << 20 | ($h >> 16 & 15) << 16;
$k |= ($i >> 14 & 15) << 28 | ($h >> 24 & 15) << 24;
$j .= $this->tkd($k);
return $j;
}
function tkc(){
$l = 0;
foreach(func_get_args() as $val){
$val &= 4294967295;
$val += $val > 2147483647 ? -4294967296 : ($val < -2147483647 ? 4294967296 : 0);
$l += $val;
$l += $l > 2147483647 ? -4294967296 : ($l < -2147483647 ? 4294967296 : 0);
}
return $l;
}
function tkd($l){
$l = $l > 0 ? $l : $l + 4294967296;
$m = "$l"; //deve ser uma string
$o = 0;
$n = false;
for($p = strlen($m) - 1; $p >= 0; --$p){
$q = $m[$p];
if($n){
$q *= 2;
$o += floor($q / 10) + $q % 10;
} else {
$o += $q;
}
$n = !$n;
}
$m = $o % 10;
$o = 0;
if($m != 0){
$o = 10 - $m;
if(strlen($l) % 2 == 1){
if ($o % 2 == 1){
$o += 9;
}
$o /= 2;
}
}
return "$o$l";
}
function tke($l){
$m = 5381;
for($o = 0; $o < strlen($l); $o++){
$m = $this->tkc($m << 5, $m, ord($l[$o]));
}
return $m;
}
function tkf($l){
$m = 0;
for($o = 0; $o < strlen($l); $o++){
$m = $this->tkc(ord($l[$o]), $m << 6, $m << 16, -$m);
}
return $m;
}
}
?>
The result is a goo.gl shortened link that is different each time. Then lastly i want to pass this into the header (on the top of the php file).
<?php
$picture="http://www.mysite.com/myimage.gif"; //url to a picture - e.g. http://www.mysite.com/mypic.gif
$link="http://goo.gl/blahblahblah"; //your link.
?>
So goo.gl/blahblahblah would redirect to mysite.com/?(random_string)
an ambitious task, but if anyone can help that would be great
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前的代码几乎可以工作。
但是,这需要在类定义之后执行。
Your current code almost works.
However, this needs to be executed after the class is defined.