Skip to main content
1 de 2
A. Cedano
  • 95.2k
  • 31
  • 145
  • 257

A mi me funciona usando cURL. Al principio recibía caracteres extraños, aún poniendo el header, pero lo solucioné con esta opción: curl_setopt($ch, CURLOPT_ENCODING ,"");

header("Content-type: application/json; charset=utf-8");    
$request='https://api.stackexchange.com/2.2/posts/245813/revisions?site=es.stackoverflow';
$ch = curl_init($request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_ENCODING ,"");
$response = curl_exec($ch);
$mJson=json_decode($response);
var_dump($mJson);
if(curl_errno($ch))
{
    echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);

Salida:

object(stdClass)#1 (4) {
  ["items"]=>
  array(2) {
    [0]=>
    object(stdClass)#2 (10) {
      ["user"]=>
      object(stdClass)#3 (6) {
        ["reputation"]=>
        int(686)
        ["user_id"]=>
        int(119615)
        ["user_type"]=>
        string(10) "registered"
        ["profile_image"]=>
        string(45) "https://i.sstatic.net/AHjNA.jpg?s=128&g=1"
        ["display_name"]=>
        string(5) "David"
        ["link"]=>
        string(47) "https://es.stackoverflow.com/users/119615/david"
      }
      ["set_community_wiki"]=>
      bool(false)
      ["is_rollback"]=>
      bool(false)
      ["creation_date"]=>
      int(1552603063)
      ["post_id"]=>
      int(245813)
      ["post_type"]=>
      string(8) "question"
      ["revision_type"]=>
      string(11) "single_user"
      ["revision_number"]=>
      int(2)
      ["comment"]=>
      string(38) "El código PHP no se ejecuta en SO"
      ["revision_guid"]=>
      string(36) "E6C4E505-3F15-4761-95A4-039F60526EFB"
    }
    [1]=>
    object(stdClass)#4 (11) {
      ["tags"]=>
      array(3) {
        [0]=>
        string(3) "php"
        [1]=>
        string(5) "mysql"
        [2]=>
        string(4) "post"
      }
      ["user"]=>
      object(stdClass)#5 (6) {
        ["reputation"]=>
        int(17)
        ["user_id"]=>
        int(113447)
        ["user_type"]=>
        string(10) "registered"
        ["profile_image"]=>
        string(87) "https://www.gravatar.com/avatar/ca122f2249fa55f5e3e12156c9c4e2fd?s=128&d=identicon&r=PG"
        ["display_name"]=>
        string(8) "jonathan"
        ["link"]=>
        string(50) "https://es.stackoverflow.com/users/113447/jonathan"
      }
      ["set_community_wiki"]=>
      bool(false)
      ["is_rollback"]=>
      bool(false)
      ["creation_date"]=>
      int(1552594218)
      ["post_id"]=>
      int(245813)
      ["post_type"]=>
      string(8) "question"
      ["revision_type"]=>
      string(11) "single_user"
      ["revision_number"]=>
      int(1)
      ["title"]=>
      string(42) "consulta Mysql con datos enviados por POST"
      ["revision_guid"]=>
      string(36) "1828F694-7C46-4663-A739-E987405A7542"
    }
  }
  ["has_more"]=>
  bool(false)
  ["quota_max"]=>
  int(300)
  ["quota_remaining"]=>
  int(283)
}
A. Cedano
  • 95.2k
  • 31
  • 145
  • 257