-1

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
  • What is the criteria for selecting the specific text you want? e.f. is it the value of the session parameter, is it the value first parameter regardless of which parameter it is, etc? Also you seem to be asking 2 questions here - Q1 is how to select the part you want and Q2 is how to write it to the file. Please note that each question on Stack Overflow should be one single question. Commented Aug 27, 2023 at 17:10
  • // Use substr() again to get the part of the string before the semicolon - what semicolon? There doesn't appear to be any in the example input value you have given.
    – C3roe
    Commented Aug 28, 2023 at 6:19

2 Answers 2

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");
3
  • it there a way to implement that on the same php curl which I mentioned above & print that session "XRpC8tmn2_VUz6temt_j19BIGymiYyd5ZOLoWtp1H5U" to my file 'xxx'
    – PachaMan
    Commented Aug 27, 2023 at 2:40
  • Curl is not mentioned in the original post, but would that not simply be: file_put_contents("xxx", $output['session']); ... or whatever you'd prefer to call $output, as that is only an example.
    – Paul T.
    Commented Aug 27, 2023 at 2:43
  • I added an example code replacement based on your example, if that might be helpful? (if this is what you were asking about in the previous comment?)
    – Paul T.
    Commented Aug 27, 2023 at 3:40
0

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'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.