[GH-ISSUE #191] Set proxy in curl options? #128

Closed
opened 2026-02-27 19:26:12 +03:00 by kerem · 2 comments
Owner

Originally created by @Marixlive on GitHub (May 9, 2020).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/191

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

Originally created by @Marixlive on GitHub (May 9, 2020). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/191 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. ```php $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. ```php $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. ```php $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 <?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
kerem 2026-02-27 19:26:12 +03:00
Author
Owner

@Marixlive commented on GitHub (May 9, 2020):

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

<!-- gh-comment-id:626199992 --> @Marixlive commented on GitHub (May 9, 2020): i use version 3.4.0 and this is the doc site [Setting custom cURL options ](https://github.com/jwilsson/spotify-web-api-php/blob/master/docs/examples/setting-custom-curl-options.md)
Author
Owner

@jwilsson commented on GitHub (May 9, 2020):

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!

<!-- gh-comment-id:626218448 --> @jwilsson commented on GitHub (May 9, 2020): 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: ```php $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` instance` to `Session`. I hope this solves your issue!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-web-api-php#128
No description provided.