Extract contents from <script>

Asked

Viewed 53 times

1

How can I be extracting the value of "csrf_token" from this code on the page:

EDIT: There was one missing } at the end of string of variable window._sharedData, so the json string was invalid.

<script type="text/javascript">window._sharedData = {"activity_counts":null,"config":{"csrf_token":"WrNBfRGzWkUl4tsZsqm1HfwhDUDKNFts","viewer":null},"supports_es6":true,"country_code":"unknown","language_code":"pt-br","locale":"pt_BR","entry_data":{"LoginAndSignupPage":[{"captcha":{"enabled":false,"key":""},"gdpr_required":false,"tos_version":"row","username_hint":""}]},"gatekeepers":{"ld":true,"seo":true,"seoht":true,"sf":true,"saa":true},"knobs":{"acct:ntb":0,"cb":0,"captcha":0}}

I tried this way, but I didn’t succeed:

$data = curl_exec($ch);
          $json = json_decode($data, true);
          echo  $json["config"][0]["csrf_token"];

1 answer

1


<?php
// passando o json diretamente para poder testar
$data = '{"activity_counts":null,"config":{"csrf_token":"WrNBfRGzWkUl4tsZsqm1HfwhDUDKNFts","viewer":null},"supports_es6":true,"country_code":"unknown","language_code":"pt-br","locale":"pt_BR","entry_data":{"LoginAndSignupPage":[{"captcha":{"enabled":false,"key":""},"gdpr_required":false,"tos_version":"row","username_hint":""}]},"gatekeepers":{"ld":true,"seo":true,"seoht":true,"sf":true,"saa":true},"knobs":{"acct:ntb":0,"cb":0,"captcha":0}}';

$json = json_decode($data); // transformando string json em objeto

// acessando atributo da chave desejada
var_dump($json->{'config'}->{'csrf_token'});
?>

I hope I’ve helped you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.