我有以下代码:
preg_match_all('/"([^"]*)"/', $json , $results);
var_dump($json);var_dump($results);die();
此时转储$json
已
string(423) "{"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX355_.jpg";[355,266],"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX425_.jpg":[425,319],"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX466_.jpg":[466,350],"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX450_.jpg":[450,338],"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL.jpg":[500,375]}"
我正在尝试获取链接。我已经尝试了json_decode
,但我得到了错误号4,这是错误的语法。字符串上的JSON前面或后面没有不可见的字符。没有运气,我决定尝试正则表达式,但上面的代码返回
array(2) { [0]=> array(0) { } [1]=> array(0) { } }
非常感谢任何获得第一名的帮助。
好吧,正如你们中的一些人所说的那样,无论如何,这基本上都是让它无法运作的黑客。如果您对此感兴趣,请访问以下完整信息:
$ch = curl_init("http://www.amazon.com/gp/product/B00BEL2G4C/ref=s9_wish_gw_d31_g21_i3?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=desktop-1&pf_rd_r=1VPYMKFSFN5BRHD4AD3W&pf_rd_t=36701&pf_rd_p=1970559082&pf_rd_i=desktop");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true );
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt" );
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt" );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
$curl_scraped_page = curl_exec($ch);
$html = $html->load($curl_scraped_page);
$json = $html->find('#imageBlock', 0)->children[0]->children[0]->children[1]->children[1]->children[0]->children[2]->children[0]->children[0]->children[0]->children[0]->children[0]->attr['data-a-dynamic-image'];
$json = utf8_encode($json);
var_dump(json_decode($json));var_dump(json_last_error());die();
我知道亚马逊有一个API,但它们很烦人,如果你是一个联盟会员,它们只会让你使用它而且他们不接受建设网站作为附属公司所以我只是试图解决这个问题并且会改变一旦网站上线并获得亚马逊附属公司的批准,它就会进入API。
URL实际上是动态的,只是用于静态测试。 我会喜欢找到一个JSON解决方案,因为它会更清晰。
答案 0 :(得分:2)
这样做 -
$parsed = json_decode($json, true);
foreach($parsed as $row=>$value){
echo $row .'<br>';
}
答案 1 :(得分:1)
不确定为什么它不起作用。这对我有用:
<?php
$json ='{"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX355_.jpg": [355,266],"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX425_.jpg":[425,319],"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX466_.jpg":[466,350],"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX450_.jpg":[450,338],"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL.jpg":[500,375]}';
preg_match_all('/"([^"]*)"/', $json , $results);
var_dump($json);var_dump($results);die();
?>
输出结果为:
gregp:~ greg$ php ./test.preg.php
string(373) "{"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX355_.jpg":[355,266],"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX425_.jpg":[425,319],"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX466_.jpg":[466,350],"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX450_.jpg":[450,338],"http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL.jpg":[500,375]}"
array(2) {
[0]=>
array(5) {
[0]=>
string(65) ""http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX355_.jpg""
[1]=>
string(65) ""http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX425_.jpg""
[2]=>
string(65) ""http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX466_.jpg""
[3]=>
string(65) ""http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX450_.jpg""
[4]=>
string(57) ""http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL.jpg""
}
[1]=>
array(5) {
[0]=>
string(63) "http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX355_.jpg"
[1]=>
string(63) "http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX425_.jpg"
[2]=>
string(63) "http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX466_.jpg"
[3]=>
string(63) "http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL._SX450_.jpg"
[4]=>
string(55) "http://ecx.images-amazon.com/images/I/51Lg%2Bd4cqRL.jpg"
}
}
答案 2 :(得分:1)
只是在“的
之间进行非贪婪的匹配preg_match_all('/"(.*?)"/', $json , $results);
var_dump($json);var_dump($results);die();
答案 3 :(得分:0)
所以我必须在调用解码之前做$json = preg_replace('/"/', '"', $json);
并修复我的问题。