Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Php Regexp未按预期工作_Php_Json_Regex_Curl - Fatal编程技术网

Php Regexp未按预期工作

Php Regexp未按预期工作,php,json,regex,curl,Php,Json,Regex,Curl,我想取产品的sku 请帮我写这个的regexp 这是我的密码- $url = "http://api.findify.io/v1.0/store/search?callback=jQuery111206735094679573879_1458022087824&q=154701001&key=5b31ee91-78fa-48e1-9338-1748ca55028e&analytics%5Bkey%5D=5b31ee91-78fa-48e1-9338-1748ca55028

我想取产品的sku

请帮我写这个的regexp

这是我的密码-

$url = "http://api.findify.io/v1.0/store/search?callback=jQuery111206735094679573879_1458022087824&q=154701001&key=5b31ee91-78fa-48e1-9338-1748ca55028e&analytics%5Bkey%5D=5b31ee91-78fa-48e1-9338-1748ca55028e&analytics%5Bvisit%5D=true&analytics%5Buniq%5D=true&analytics%5Burl%5D=http%253A%252F%252Fwww.pandorasoem.com%252Fsearch%2523q%253D154637401&analytics%5Bbaseurl%5D=http%253A%252F%252Fwww.pandorasoem.com%252Fsearch%2523q%253D154637401&analytics%5Bhost%5D=www.pandorasoem.com&analytics%5Bwidth%5D=1920&analytics%5Bheight%5D=1200&analytics%5Binner_width%5D=1438&analytics%5Binner_height%5D=667&analytics%5Bdoc_width%5D=1438&analytics%5Bdoc_height%5D=915&analytics%5Bscroll_x%5D=0&analytics%5Bscroll_y%5D=0&analytics%5Bvisit_id%5D=Ts22zuHHGJRZc3U1&analytics%5Buniq_id%5D=BoeCUKSzgdML6C50&byPage=24&page=0&_=1458022087825";
$ch1= curl_init();
curl_setopt ($ch1, CURLOPT_URL, $url );
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1,CURLOPT_VERBOSE,1);
curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
curl_setopt ($ch1, CURLOPT_REFERER,'http://www.google.com');  //just a fake referer
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1,CURLOPT_POST,0);
//curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 20);
$htmlContent= curl_exec($ch1);
curl_close($ch1);
preg_match_all('/["\']?totalHits["\']?\s*:\s*(\d+)/i', $htmlContent, $count);
print_r($count);    

preg_match_all('/"sku".*:.*"(.*)".*/i', $htmlContent, $sku);
print_r($sku);
它在获取sku时显示空白数组。它适用于Totalhits


只需从URL中删除回调参数,它将返回正确的JSON,然后您就可以将JSON转换为数组或对象,并执行任何您想要的操作

从URL中删除
callback=jquery11206735094679573879_1458022087824&

要将JSON转换为数组,请使用
JSON\u decode
()

因此,在获得
$htmlContent
后,可以使用
$jsonData=json\u decode($htmlContent,true)对其进行转换

您可以
print\r
json检查其键/值

或者,如果您想获取
sku
-s的列表

$skus = Array();
foreach($jsonData['data']['hits'] as $hit) {
    $skus[] = $hit['sku'];
}

您的
sku
响应是一个数组(而您的
totalHits
实际上是一个数字,在本例中为3),请使用
JSON
解析器而不是正则表达式。@Jan我不知道如何使用JSON解析器。你能给我举个例子吗这是一个
jQuery
响应,所以这里:Json是空的。。在print_r($jsonData)和使用foreach代码之后,我没有得到任何东西。还尝试打印$SKU。然后显示
PHP警告:为foreach()提供的参数无效
您是否已从URL中删除回调?