0

Hey friends I want to edit this line through php and want to get only access token how it works I really don't know........ Thanks in advance here is the line whom I want to edit and get access token only after editing

{"session_key":"5.Gdl_wBHvvGgqgg.1492451185.6-100009065037646","uid":100009065037646,"secret":"6ead07f9e86b834479ce5db2","access_token":"EAAAAAYsX7TsBAO1ciZAeWUDDvXGpgpI4DZCZBPFGbKHVxETEuniiRRyoyHQsgmSnbz3EL2QWLxGRK5Omhc6NL9VLkDxMZBIomIbKfFLZAqcy2ueillVrehoB3Sh95mcSalblAs0Pmj4PrcKZBZC1KVWJ0Hv2S1NDL2OTXR2Pp9vsUGEAFbpEaJ7GDTbQyDjy70hSp7wZDZD","machine_id":"cf_0WHKKYqcQK6mM0akSiy0P","confirmed":true,"identifier":"9472531429 "}

and here is my php script from which I want to edit this line and get only access token instead of this whole line

if(preg_match("'access_token=(.*?)&expires_in='", $token2, $matches)){
    $token = $matches[1];
        }else{
    $token = $token2;}
    $exe = json_decode(get_html("https://graph.facebook.com/app?access_token=".$token ))->id;
  • You can use [PHP's string functions](http://php.net/manual/en/ref.strings.php). First find `"access_token":`, then read everything between the following `""`. Check out [`strpos()`](http://php.net/manual/en/function.strpos.php) and [`substr()`](http://php.net/manual/en/function.substr.php). – domsson Apr 17 '17 at 17:52
  • Can you plz give me example because I am new in php @domdom – Avinash Kumar Apr 17 '17 at 17:53
  • 1
    What do you mean by "I want to edit"? – arkascha Apr 17 '17 at 17:53
  • 1
    BTW: that is a JSON encoded object, you want to decode it. – arkascha Apr 17 '17 at 17:54
  • I only want to know that how can I get only this acces token by php after removing the rest of the line – Avinash Kumar Apr 17 '17 at 17:55
  • In case you don't want to use `json_decode()` for some reason: http://ideone.com/48nRy5 – domsson Apr 17 '17 at 18:04

1 Answers1

0
$line = '{"session_key":"5.Gdl_wBHvvGgqgg.1492451185.6-100009065037646","uid":100009065037646,"secret":"6ead07f9e86b834479ce5db2","access_token":"EAAAAAYsX7TsBAO1ciZAeWUDDvXGpgpI4DZCZBPFGbKHVxETEuniiRRyoyHQsgmSnbz3EL2QWLxGRK5Omhc6NL9VLkDxMZBIomIbKfFLZAqcy2ueillVrehoB3Sh95mcSalblAs0Pmj4PrcKZBZC1KVWJ0Hv2S1NDL2OTXR2Pp9vsUGEAFbpEaJ7GDTbQyDjy70hSp7wZDZD","machine_id":"cf_0WHKKYqcQK6mM0akSiy0P","confirmed":true,"identifier":"9472531429 "}';

$decoded = json_decode($line,true);
echo $decoded['access_token'];

This code must do the job. You shouldn't need preg_match, for your case.

Оzgur
  • 432
  • 2
  • 10