Php 通过带卷曲引号的标记检索美味的JSON数据
我创建了一个脚本,它从以下位置检索数据:Php 通过带卷曲引号的标记检索美味的JSON数据,php,json,curl,delicious-api,Php,Json,Curl,Delicious Api,我创建了一个脚本,它从以下位置检索数据: ...retrieve usernames and such... $username = 'randomUser'; $parentTag = array("tag'12","tag’12"); //note the different quotes being used! $amount = 100; foreach ($parentTag as $pTag){ $url = "http://feeds.delicious.com/v2/
...retrieve usernames and such...
$username = 'randomUser';
$parentTag = array("tag'12","tag’12"); //note the different quotes being used!
$amount = 100;
foreach ($parentTag as $pTag){
$url = "http://feeds.delicious.com/v2/json/".$username."/".$pTag."?count=".$amount;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
...store data to DB...
}
当手动访问两个链接(使用不同的标记)时,Delicious会显示不同的数据,因为它将这两个链接视为单独/不同的标记,并且用户使用了这两种类型的引号。当使用我的脚本访问数据时,第一个脚本工作,但第二个脚本失败,并且根本不显示任何数据
我尝试了urlencoding和rawurlencoding标记,手动将卷曲引号替换为’;和%92,但没有用
因此,问题是:
如何更改脚本以从Delicious获取两个标记的JSON数据?这不是真正的答案。但对我来说,这很管用:
<?php
// Used sprintf to keep it readable, shouldn't make a difference
$urlTemplate = 'http://feeds.delicious.com/v2/json/%s/%s?count=%d';
$tag = 'tag’12';
$url = sprintf($urlTemplate, 'wjzijderveld', $tag, 20);
echo $url . PHP_EOL;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
var_dump(json_decode(curl_exec($ch))).PHP_EOL;
curl_close($ch);
终于找到了这个问题的解决方案,将来可能会对其他人有所帮助。不确定这是否是这个问题的最佳解决方案,但它奏效了
威廉·简为我指明了编码的正确方向。我使用Firefox的附加Live HTTP头来调查URL发送到Delicious的方式。“链接”http://feeds.delicious.com/v2/json/username/tag'12?计数=100“转换为”http://feeds.delicious.com/v2/json/username/tag%E2%80%9912?count=100“
当使用已翻译的URL作为$URL变量时,请求显示正确的结果 您在$url=
行的开头缺少一个撇号谢谢,在复制代码时遗漏了这个撇号不知道为什么,但Google Chrome似乎将标记'12编码为标记%E2%80%9912。(不是%92)我尝试使用您的示例,但得到的输出是:'12?计数=100 NULL