mremi / UrlShortener

A PHP library to generate shortened URL through famous API like Bit.ly or Google
MIT License
79 stars 32 forks source link

Allow specify cURL options for connections to providers #25

Open gnadelwartz opened 4 years ago

gnadelwartz commented 4 years ago

Hello mremi,

I must use UrlShortner with socks5 for some reasons. Unfortunately the typical shell hackers solution using proxychains shorten ... does not work with php compiled against glibc.

The solution for my own calls to cURL is to provide some cURL options:

                curl_setopt($this->handler, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
                curl_setopt($this->handler, CURLOPT_PROXY, 'localhost');

unfortunately - for me - you are using guzzle so I can't set CURLOPT direct, but luckly guzzle privides the possiblilty to pass cURL options

As UrlShortner is working with user and password on my server, I tried to add the the socks5 options to your bitly provider for shortening, but it has ho effect:

    public function shorten(LinkInterface $link, $domain = null)
    {
        $client = $this->createClient();

        $this->options['headers']['Authorization'] = 'Bearer '.$this->auth->getAccessToken();

        $response = $client->post('/v4/shorten', array_merge([
            'json' => [
                'domain'   => $domain,
                'long_url' => $link->getLongUrl(),
            ],

       'curl' => [
            CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5_HOSTNAME,
            CURLOPT_PROXY => 'localhost'
        ],

        ], $this->options));

        $response = $this->validate($response);

        $link->setShortUrl($response->link);
    }

Are I'm doing something fundamentally worng or did I miss something?

Any other ideas?

gnadelwartz commented 4 years ago

OK found the Problem, UrlShortner needs to first geht the oAuth key, so I have to add it there also:

    public function getAccessToken()
    {
        $client = new Client( [
            'base_uri' => 'https://api-ssl.bitly.com/oauth/access_token',
       'curl' => [
            CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5_HOSTNAME,
            CURLOPT_PROXY => 'localhost'
        ],
        ]);

        $response = $client->post(null, [
            'auth' => [
                $this->username,
                $this->password,
            ],
        ]);

        return $response->getBody()->getContents();
    }
gnadelwartz commented 4 years ago

Even I found a solution to my Problem, it would be nice to add the possibility to set cUrl options globaly for all modules if needed.

mremi commented 4 years ago

Hi @gnadelwartz ,

Yes you're right, we could pass options in construct as the provider. Could you open a PR in this way please?

gnadelwartz commented 4 years ago

even it may not look like, I'm a noob in php programing :)

I can write simple php scripts and copy solutions from stack overflow or find simple fixes, but if it comes to real php incl. OO I have to give up.

I'm more a Shell hacker from stone age