View difference between Paste ID: s288YsQz and PSDfbfpv
SHOW: | | - or go back to the newest paste.
1
<?php
2
$apikey = "";
3
?>
4
<html>
5
    <head>
6
        <title>goo.gl checker</title>
7
    </head>
8
    <body>
9
        <?
10
        if (isset($_POST['domains'])):
11
            $domains = explode("\n", $_POST['domains']);
12
13
            $googl = new Googl($apikey);
14
15
            foreach ($domains as $domain):
16
                $clean = trim($domain);
17
                if(!$clean):
18
                    continue;
19
                endif;
20
                
21
                $short = $googl->shorten('http://'.$clean);
22
                echo "<h6>$clean - <a href='$short' target='_blank'>$short</a> - ";
23
                $ch = curl_init($short);
24
                curl_setopt($ch, CURLOPT_VERBOSE, 0);
25
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
26
                curl_exec($ch);
27
                $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
28
                echo "<i>$status</i> - ";
29
                if($status > 200):
30
                    echo "<span style='color:green;'>OK</span>";
31
                else:
32
                    echo "<span style='color:red;'>BAD</span>";
33
                endif;
34
                echo "</h6>";
35
                curl_close($ch);
36
            endforeach;
37
        endif;
38
        ?>
39
        <form method="post">
40
            <textarea name="domains" rows="30" cols="50"></textarea>
41
            <br/>
42
            <button type="submit">go</button>
43
        </form>
44
    </body>
45
</html>
46
<?
47
48
/**
49
 * This file is part of googl-php
50
 *
51
 * https://github.com/sebi/googl-php
52
 *
53
 * googl-php is free software: you can redistribute it and/or modify
54
 * it under the terms of the GNU General Public License as published by
55
 * the Free Software Foundation, either version 3 of the License, or
56
 * (at your option) any later version.
57
 *
58
 * This program is distributed in the hope that it will be useful,
59
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
61
 * GNU General Public License for more details.
62
 *
63
 * You should have received a copy of the GNU General Public License
64
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
65
 */
66
class Googl
67
{
68
69
    public $extended;
70
    private $target;
71
    private $apiKey;
72
    private $ch;
73
    private static $buffer = array();
74
75
    function __construct($apiKey = null)
76
    {
77
        # Extended output mode
78
        $extended = false;
79
80
        # Set Google Shortener API target
81
        $this->target = 'https://www.googleapis.com/urlshortener/v1/url?';
82
83
        # Set API key if available
84
        if ($apiKey != null)
85
        {
86
            $this->apiKey = $apiKey;
87
            $this->target .= 'key=' . $apiKey . '&';
88
        }
89
90
        # Initialize cURL
91
        $this->ch = curl_init();
92
        # Set our default target URL
93
        curl_setopt($this->ch, CURLOPT_URL, $this->target);
94
        # We don't want the return data to be directly outputted, so set RETURNTRANSFER to true
95
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
96
    }
97
98
    public function shorten($url, $extended = false)
99
    {
100
101
        # Check buffer
102
        if (!$extended && !$this->extended && !empty(self::$buffer[$url]))
103
        {
104
            return self::$buffer[$url];
105
        }
106
107
        # Payload
108
        $data = array('longUrl' => $url);
109
        $data_string = '{ "longUrl": "' . $url . '" }';
110
111
        # Set cURL options
112
        curl_setopt($this->ch, CURLOPT_POST, count($data));
113
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data_string);
114
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, Array('Content-Type: application/json'));
115
116
        if ($extended || $this->extended)
117
        {
118
            return json_decode(curl_exec($this->ch));
119
        }
120
        else
121
        {
122
            $ret = json_decode(curl_exec($this->ch))->id;
123
            self::$buffer[$url] = $ret;
124
            return $ret;
125
        }
126
    }
127
128
    public function expand($url, $extended = false)
129
    {
130
        # Set cURL options
131
        curl_setopt($this->ch, CURLOPT_HTTPGET, true);
132
        curl_setopt($this->ch, CURLOPT_URL, $this->target . 'shortUrl=' . $url);
133
134
        if ($extended || $this->extended)
135
        {
136
            return json_decode(curl_exec($this->ch));
137
        }
138
        else
139
        {
140
            return json_decode(curl_exec($this->ch))->longUrl;
141
        }
142
    }
143
144
    function __destruct()
145
    {
146
        # Close the curl handle
147
        curl_close($this->ch);
148
        # Nulling the curl handle
149
        $this->ch = null;
150
    }
151
152
}