Get Json response from URL

Asked

Viewed 189 times

0

  • 1

    it would be interesting to put what you tried to do so we could help you

  • You’re probably not getting it because the site blocks some requests and asks to confirm that it’s not a robot.

  • @Valdeirpsr But how can https://jsonformatter.org/ do it?

1 answer

0


You’re probably not getting it because the site blocks some requests and asks to confirm that it’s not a robot.

This protection works basically as follows:

  1. Upon entering the site, the code checks whether or not the request has the cookie cf_clearance with a token valid, if it does not display the reCaptcha google.

  2. After the user confirms that he is not a robot, the server returns a cookie with a token.

Already the site jsonformatter.org, probably uses cookies to circumvent this protection. In php, would be more or less that way:

<?php

/* Inicia a biblioteca cURL com a URL */
$ch = curl_init("https://www.cryptocompare.com/api/data/coinsnapshotfullbyid/?id=1182");

/* Informa que deseja capturar o resultado */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

/* Desabilita a verificação do certificado SSL */
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

/* Informa o Cookie com o token válido para burlar a proteção */
curl_setopt($ch, CURLOPT_COOKIE, "cf_clearance=3def377bda6f9be22251eff64cb617c157011246-1518023345-604800");

/* Define o User-Agent para burlar a proteção */
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");

/* Captura a resposta */
$response = curl_exec($ch);

/* Captura os erros, caso haja */
$error = curl_error($ch);

/* Captura as informações da requisição */
$info = curl_getinfo($ch);

/* Fecha a requisição e exibe o resultado */
curl_close($ch);

echo($response);
  • Perfect, that’s right! I had to put the other cookies too, separating by " ; ". Thank you so much for the help...

Browser other questions tagged

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