[php-src] Issue #7909: Php curl doesnt redirect

From: Date: Fri, 07 Jan 2022 19:30:39 +0000
Subject: [php-src] Issue #7909: Php curl doesnt redirect
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-238864@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/7909 Author: ejgutierrez74 ### Description The following code or in general any public api like spotify, twitter that sends a response with redirect url: ```<?php session_start(); ob_start(); function random() { return (float)rand()/(float)getrandmax(); } /** * Generates a random string containing numbers and letters * @param {number} length The length of the string * @return {string} The generated string */ function generateRandomString ( $length) { $text = ''; $possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; for ($i = 0; $i < $length; $i++) { $text[$i]=$possible[intval(floor(random() * strlen($possible)))]; } return base64_encode($text); }; /* *The userid and password are separated by a single colon (":") character within a base64 encoded string in the credentials. If the user agent wishes to send the Client ID "Aladdin" and password "open sesame,” it would use the following header field: Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== To create the basic authorization header you will need to base64 encoding on your Client ID and Client Secret which can be obtained from your App’s “Keys and Tokens” page inside of the developer portal.*/ $client_id='cFo1S1g4azUweVJzWFBBcEx1LVE6MTpjaQ'; $client_secret='OdvBpQG60W5UPpRMyAIPMSFZc2zWf8NcGIXCcPs_bdaz7906IK'; $authorizationHeader = 'Authorization: Basic ' . base64_encode($client_id . ':' . $client_secret); /* Steps to connect using OAuth 2.0 Step 1: Construct an Authorize URL Your App will need to build an authorize URL to Twitter, indicating the scopes your App needs to authorize. For example, if your App needs to lookup Tweets, users and to manage follows, it should request the following scopes: tweet.read%20users.read%20account.follows.read%20account.follows.write The URL will also contain the code_challenge and state parameters, in addition to the other required parameters. In production you should use a random string for the code_challenge. */ $code_challenge = generateRandomString(8); $_SESSION['codechallengesaved']=$code_challenge; //state tb random ??? $state= 'state'; $_SESSION['statesaved']=$state; //%20 sino en espacio en blanco. si quieres offline acces se lo añades, si no por defecto no pones nada y es online access $scope="tweet.read users.read follows.read follows.write offline.access"; //$scope="tweet.read%20users.read%20account.follows.read%20account.follows.write%20offline.access"; /* Step 2: GET oauth2/authorize Have the user authenticate and send the application an authorization code. If you have enabled OAuth 2.0 for your App you can find your Client ID inside your App’s “Keys and Tokens” page. An example URL to redirect the user to would look like this: https://twitter.com/i/oauth2/authorize?response_type=code &client_id=M1M5R3BMVy13QmpScXkzTUt5OE46MTpjaQ &redirect_uri=https://www.example.com &scope=tweet.read%20users.read%20follows.read%follows.write &state=state &code_challenge=challenge &code_challenge_method=plain */ $endpoint='https://twitter.com/i/oauth2/authorize'; //$redirect_uri='http://localhost:8080/callback/logintwitter.php'; $redirect_uri='https://votacionya.000webhostapp.com/callback/logintwitter.php'; //$redirect_uri='http://127.0.0.1:8080/callback/logintwitter.php'; $url = $endpoint . '?' . 'response_type=' . 'code' . '&client_id=' . $client_id . '&redirect_uri=' . rawurlencode($redirect_uri) . '&scope=' . rawurlencode($scope) . '&state=' . $state . '&code_challenge=' . $code_challenge . '&code_challenge_method=' . 'plain'; //Inicio curl $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPGET, true); //No se mucho que hacen estas opciones pero funcionan curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0"); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //resultado de peticion $result = curl_exec($ch); //Gestion de errores if (curl_errno($ch)) { print "Error: " . curl_error($ch); echo 'Error en ch curl'; } else { // Show me the result Info de la peticion CURL ...se podria quitar mas adelante echo 'ch curl Info'; echo '<h4> Info Curl </h4>'; $info = curl_getinfo($ch); echo '</br>'; echo '<pre>' . print_r($info, true) . '</pre>'; $json = json_decode($result); //Resultado consulta...puedo acceder de la forma json->access_token echo '</br>'; echo '<h2> Resultado consulta </h2>'; echo '<pre>' . print_r($json, true) . '</pre>'; echo '<pre>' . print_r($json->code, true) . '</pre>'; echo '<pre>' . print_r($json->state, true) . '</pre>'; echo '</br>'; var_dump($result); } //Cierro CURl curl_close($ch); //Importante dode reenviO, EL REDIRECT URI ha de coincidir con el redirect de tu aplicacion // header('Access-Control-Allow-Origin : *'); header('Location: '.$url); ob_end_flush(); ``` Resulted in this output: Spotify API have to redirect to the uri. Instead you have to insert header('Location: '.$url); at the end, that is the instruction that redirects you. But I expected this output instead: php curl should receive the data from spotify api or whatever, and redirect to the url ``` ``` ### PHP Version 7.4 ### Operating System _No response_

« previous php.bugs (#238864) next »