【问题标题】:I'm having trouble POSTING a JSON string to a url for the output in JSON我在将 JSON 字符串发布到 JSON 输出的 url 时遇到问题
【发布时间】:2012-08-14 00:34:39
【问题描述】:

我正在尝试向此 URL 发出发布请求:

http://clan.z8games.com/clanServices.asmx/getPlayerPublicInfo

使用这个 JSON 字符串

{"usn":12434525}

得到与此类似的输出;

 {"d":"{\"usn\":12434525,\"nick\":\"Guiness\",\"lev\":39,\"playCnt\":3734,\"winCnt\":3210,\"loseCnt\":239,\"enemyKillCnt\":129734,\"deathCnt\":5140,\"exp\":1072522,\"headshotKillCnt\":120339,\"friendKillCnt\":5,\"escapeCnt\":45,\"regDate\":\"2011\",\"lastPlayDate\":\"2012-08-12\",\"guildid\":217089,\"clanName\":\"Guiness™\",\"memberType\":null,\"gcClanID\":55000,\"hs5p\":202,\"fk1p\":0,\"k10p\":211,\"d20p\":2,\"esc4p\":0,\"sv3p\":676,\"matchLosses\":98,\"matchWins\":1894}"}

因为我为游戏帐户(如统计数据)制作了一个数据抓取器,并且每个页面根据帐户 ID 不同。

我试过了:

<?php 
$usn=$_GET["accountUSN"]; 
$url='http://clan.z8games.com/clanServices.asmx/getPlayerPublicInfo';

$options = array(
    'http' => array(
        'method' => "POST",
        'content' => http_build_query(
            array(
                'usn'=>$usn
             )
        )
    )
);

$context = stream_context_create($options);
$result =  file_get_contents($url,NULL,$context);
var_dump($result);
?>

有人提出这个建议,但它总是返回

bool(false)

我只是想知道如何正确地做到这一点,如果它能让你感觉更好,我真的不想要任何完整的代码。另外,我试图避免使用 cURL,我想要一个原生选项。

另外,有人试图在这里帮助我: https://stackoverflow.com/questions/11920064/php-simple-html-dom-parser-not-giving-me-text-for-a-td 但我不小心删除了帖子,作为一个新用户。如何取消删除?

编辑:

<?php

$usn=$_GET['accountUSN'];
$url='http://clan.z8games.com/clanServices.asmx/getPlayerPublicInfo';

$options=array('http'=>array('method'=>'POST','content'=>'content' => '{\'usn\':$usn}'));

$context=stream_context_create($options);
$result=file_get_contents($url,NULL,$context);
var_dump($result);
?>

服务器错误。

【问题讨论】:

  • 您将内容作为查询字符串而不是 json 字符串发送
  • 你能解释一下吗?我不明白 :( 我查询帐号,因为它看起来像这样:clan.z8games.com/charstat_cf.aspx?usn=12434525 但在页面加载后,我在 OP 中发布的链接有一个 ajax 调用,以获取所有玩家数据。
  • http_build_query 会给你usn=12434525,如果你想要json你可以使用'content' =&gt; "{\"usn\":$usn}"
  • 我试过你说的,我得到一个服务器错误。
  • 我查看了您尝试使用的服务,它使用的是 SOAP 而不是 JSON,请参阅 clan.z8games.com/clanServices.asmx?op=getPlayerPublicInfo

标签: php json http http-post


【解决方案1】:

我使用这个的方式是打开文件,然后从文件指针中获取流内容。

$context = stream_context_create($options); 
$fp = fopen($url, 'rb', false, $context);
if (!$fp) {
   die('Failed to open stream');
}
$result = stream_get_contents($fp);
var_dump($result);

【讨论】:

    【解决方案2】:

    也许你可以使用 firebug,查看 Net 选项卡,比较你以编程方式发送的内容和浏览器发送的内容。 确保你发送了它想要的东西。

    【讨论】:

      猜你喜欢
      • 2018-12-09
      • 2013-05-22
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 2011-08-22
      • 2014-12-05
      • 2013-02-16
      • 2016-08-02
      相关资源
      最近更新 更多