jwilsson / spotify-web-api-php

A PHP wrapper for Spotify's Web API.
MIT License
862 stars 156 forks source link

Set proxy in curl options? #191

Closed Marixlive closed 4 years ago

Marixlive commented 4 years ago

Hey Man. Thanks very much for this amazing php libary. I like to use a proxy server for the api calls.

The auto_retry options works perfectly if i use this code.

$session = new SpotifyWebAPI\Session('ID','SECRET');

$options = [
    'auto_retry' => true,
];

$api = new SpotifyWebAPI\SpotifyWebAPI();

$session->refreshAccessToken($account_refresh_token);

$accessToken = $session->getAccessToken();
$api->setOptions($options);

Now i use this code to set a proxy with the curl options in php but nothing happens.

$session = new SpotifyWebAPI\Session('ID','SECRET');

$options = [
    'auto_retry' => true,
    'curl_options' => [
        CURLOPT_TIMEOUT => 60,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4",
        CURLOPT_PROXY => 'PROXYIP',
        CURLOPT_PROXYPORT => '80',
        CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
        CURLOPT_PROXYUSERPWD => "PROXYUSER:PROXYPW",
            ],
        ];

$api = new SpotifyWebAPI\SpotifyWebAPI();

$session->refreshAccessToken($account_refresh_token);

$accessToken = $session->getAccessToken();
$api->setOptions($options);

I also tried to set the options on the Request object as described in the updated docs from your libary.

$session = new SpotifyWebAPI\Session('ID','SECRET');

$options = [
    'auto_retry' => true,
    'curl_options' => [
        CURLOPT_TIMEOUT => 60,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4",
        CURLOPT_PROXY => 'PROXYIP',
        CURLOPT_PROXYPORT => '80',
        CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
        CURLOPT_PROXYUSERPWD => "PROXYUSER:PROXYPW",
            ],
        ];

$request = new SpotifyWebAPI\Request($options);
$api = new SpotifyWebAPI\SpotifyWebAPI([], null, $request);

$session->refreshAccessToken($account_refresh_token);

$accessToken = $session->getAccessToken();

i cant get this to work, do you have any ideas?

i use this script to test a php curl call over a http web proxy. this example is working.

<?php
$url = 'https://api.myip.com/';
$proxyIP = 'PROXYIP';
$proxyPort = '80';
$proxyUsername = 'PROXYUSER';
$proxyPassword = 'PROXYPW';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36");
curl_setopt($ch, CURLOPT_PROXY, $proxyIP);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxyPort);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$proxyUsername:$proxyPassword");

$output = curl_exec($ch);

if(curl_errno($ch)){
    throw new Exception(curl_error($ch));
}

echo $output;
echo "\n";

?>

Thank you very much for help and have a nice weekend. matthias

Marixlive commented 4 years ago

i use version 3.4.0 and this is the doc site Setting custom cURL options

jwilsson commented 4 years ago

Hi! Thank you for the kind words!

I'm guessing you're not able to get an access token either? It's poorly documented, but it is possible to pass a Request instance to the Session constructor too. For example:

$requestOptions = [
  // All cURL options...
];

$options = [
  'auto_retry' => true
];

$request = new SpotifyWebAPI\Request($options);

$session = new SpotifyWebAPI\Session('ID', 'SECRET', 'REDIRECT_URI or empty string', $request);

$api = new SpotifyWebAPI\SpotifyWebAPI([], null, $request);

curl_options aren't passed on to the Request instance and auto_retry needs to be set on the SpotifyWebAPI instance. I'll try to make this more clear in the documentation. I'll also update the docs with better info on how to pass a Request instancetoSession`.

I hope this solves your issue!