Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取posterous站点id,php(preg\u match或json\u decode?)=没有乐趣_Php_Preg Match_Json_Posterous - Fatal编程技术网

获取posterous站点id,php(preg\u match或json\u decode?)=没有乐趣

获取posterous站点id,php(preg\u match或json\u decode?)=没有乐趣,php,preg-match,json,posterous,Php,Preg Match,Json,Posterous,我正在尝试从posterous获取用户的站点id。这些数据似乎不在用户配置文件中,FAQ或帮助或文档中也没有说明如何查找这些数据。 我能找到的唯一方法是通过API请求站点列表, 这就是我要做的,我得到一个json响应,实际上,用户在posterous上拥有的任何站点的站点id都在那里,我只想输出这个 首先我尝试了(其中$result是json响应) 我什么也得不到 然后我试了一下 json_decode($result); echo $result->site_id; 我还是一无所获 我

我正在尝试从posterous获取用户的站点id。这些数据似乎不在用户配置文件中,FAQ或帮助或文档中也没有说明如何查找这些数据。 我能找到的唯一方法是通过API请求站点列表, 这就是我要做的,我得到一个json响应,实际上,用户在posterous上拥有的任何站点的站点id都在那里,我只想输出这个

首先我尝试了(其中$result是json响应)

我什么也得不到

然后我试了一下

json_decode($result);
echo $result->site_id;
我还是一无所获

我可以看到,在json响应($result)中

为什么我不能用preg_match或json_decode提取这些数据?还有其他更好的方法吗


我的完整代码粘贴在

上,Posterous list of sites API返回一个映射列表(如您所料)。因此,您需要做的是首先从列表中选择正确的项,然后读取它的
id

$result = json_decode($response);
$first_result = $result[0];
$first_result_id = $first_result["id"];
echo $first_result_id;

您可以发布API的响应吗?您的
preg_match()
调用有一些问题。首先,你失踪了。此外,函数将返回匹配的次数(1或0),而不是匹配的文本。对于捕获子模式,它可能是这样工作的:
preg_match(“/”site_id):(\d+/”,$result,$matches)$siteid=$matches[1]返回的json看起来像这样:该死…太大了,无法在评论中发布,粘贴在这里:啊哈!谢谢你,怀斯盖伊!这解决了我的问题D
json\u decode($result)
将返回一个数组(不是StdClass对象),这意味着您可能希望使用括号符号访问id:
echo$result['site\u id']
。这也可以避免使用正则表达式解析JSON的麻烦:^)
"site_id": $somenumber;
$result = json_decode($response);
$first_result = $result[0];
$first_result_id = $first_result["id"];
echo $first_result_id;