skip to Main Content

I would like to print only specific part from an Url like for example if i got on response an URL like this :

https://tvoauth.zapi.com/?client=zapi-web-1&session=XRpC8tmn2_VUz6temt_j19BIGymiYyd5ZOLoWtp1H5U&scopes=false&redirect=https%3A%2F%2Ftv.zapi.com

I want to print only this part exactly :

XRpC8tmn2_VUz6temt_j19BIGymiYyd5ZOLoWtp1H5U

I have tried to use substr like this :

$string=$headers['location'][0];

$substring = substr($string, strpos($string, '=') + 1);
// Use substr() again to get the part of the string before the semicolon
$final_string = substr($substring, 0, strpos($substring, ';'));
   file_put_contents("xxx", $final_string);
   $xtoken = file_get_contents("xxx");

but it’s doesn’t print nothing to my file xxx

but if I apply it like this only :

$string=$headers['location'][0];

   file_put_contents("xxx", $string);
   $xtoken = file_get_contents("xxx");

it print full line as I mentioned above .

Hope I find the right guide to be able to select & print only that specific part I want .

thank you .

2

Answers


  1. Perhaps a combination of parse_url and then parse_str could help without needing substr?

    As an example:

    <?php
    
    $url = 'https://tvoauth.zapi.com/?client=zapi-web-1&session=XRpC8tmn2_VUz6temt_j19BIGymiYyd5ZOLoWtp1H5U&scopes=false&redirect=https%3A%2F%2Ftv.zapi.com';
    
    $parsed = parse_url($url);
    
    var_dump($parsed);
    
    $output = [];
    
    parse_str($parsed['query'], $output);
    
    var_dump($output);
    
    echo $output['session'];
    

    Gives the output:

    array(4) {
      ["scheme"]=>
      string(5) "https"
      ["host"]=>
      string(16) "tvoauth.zapi.com"
      ["path"]=>
      string(1) "/"
      ["query"]=>
      string(117) "client=zapi-web-1&session=XRpC8tmn2_VUz6temt_j19BIGymiYyd5ZOLoWtp1H5U&scopes=false&redirect=https%3A%2F%2Ftv.zapi.com"
    }
    
    array(4) {
      ["client"]=>
      string(10) "zapi-web-1"
      ["session"]=>
      string(43) "XRpC8tmn2_VUz6temt_j19BIGymiYyd5ZOLoWtp1H5U"
      ["scopes"]=>
      string(5) "false"
      ["redirect"]=>
      string(19) "https://tv.zapi.com"
    }
    
    XRpC8tmn2_VUz6temt_j19BIGymiYyd5ZOLoWtp1H5U
    

    The example is here if you’d like to try a test run.


    EDIT: from the comment about the file:
    (will not be able to write to a file in the online example)

    Then to the file can be file_put_contents("xxx", $output['session']);, or whatever one would prefer to call $output if a different name is desired.


    … based on your original code with:

    $string=$headers['location'][0];
    
    $substring = substr($string, strpos($string, '=') + 1);
    // Use substr() again to get the part of the string before the semicolon
    $final_string = substr($substring, 0, strpos($substring, ';'));
       file_put_contents("xxx", $final_string);
       $xtoken = file_get_contents("xxx");
    

    … could change to:

    $string=$headers['location'][0];
    
    $theURL = parse_url($string);
    $output = [];
    
    parse_str($theURL['query'], $output));
       file_put_contents("xxx", $output['session']);
       $xtoken = file_get_contents("xxx");
    
    Login or Signup to reply.
  2. If you are working with the URL, Just use the super global $_GET. In your question you are needing the string that pertains to the session. So to get that string

    $string = $_GET['session']; 
    file_put_contents('xxx',$string);
    

    To access the other parameters you would use:

    $client = $_GET['client'];
    $scope = $_GET['scopes'];
    $redirect = $_GET['redirect'];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search