Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 页面刮取和正则表达式在应该时不会给出结果_Php_Regex_Wordpress_Podscms - Fatal编程技术网

Php 页面刮取和正则表达式在应该时不会给出结果

Php 页面刮取和正则表达式在应该时不会给出结果,php,regex,wordpress,podscms,Php,Regex,Wordpress,Podscms,我有一个基于wp的小脚本,它使用preg_match_all()刮取一个网页并计算4个关键字的出现次数 这是我知道的包含关键字的url的代码: <?php $url ='http://www.leggioggi.it/2013/08/16/i-tre-amici-discutono-di-servizio-sanitario-casuale-e-differenze-nord-sud/'; $response = wp_remote_get($url); $the_body

我有一个基于wp的小脚本,它使用preg_match_all()刮取一个网页并计算4个关键字的出现次数

这是我知道的包含关键字的url的代码:

<?php

$url ='http://www.leggioggi.it/2013/08/16/i-tre-amici-discutono-di-servizio-sanitario-casuale-e-differenze-nord-sud/';

$response = wp_remote_get($url);

    $the_body = wp_remote_retrieve_body($response);
    //echo htmlentities($the_body);

    $matches = array();

    $matches_count = preg_match_all("/gravidanz|preconcezional|prenatal|concepimento/i", $the_body, $matches);

var_dump ($matches_count);
var_dump ($matches);
?>

我检查了$response变量(我的坏消息是没有考虑它),发现确实存在远程服务器错误,该错误在wp_remote_get()返回的响应中报告。 这就是我得到的回报:

object(WP_Error)#30 (2) {
  ["errors"]=>
  array(1) {
    ["http_request_failed"]=>
    array(1) {
      [0]=>
      string(69) "Operation timed out after 5000 milliseconds with 25692 bytes received"
    }
  }
  ["error_data"]=>
  array(0) {
  }
}
object(WP_Error)#30 (2) {
  ["errors"]=>
  array(1) {
    ["http_request_failed"]=>
    array(1) {
      [0]=>
      string(69) "Operation timed out after 5000 milliseconds with 25692 bytes received"
    }
  }
  ["error_data"]=>
  array(0) {
  }
}

这就解决了。我只需要检查http错误,并重复请求有限的次数,如果没有给出正确的响应,则忽略资源。

为了保持您收到的任何帮助的相关性,您可以发布
正文
。您确定$the_body不是空的吗?@flebel在编辑上方阅读。@Bakaburg
htmlentities(_body)不会更改任何内容。您没有将其分配给变量,因此,
$the_body
没有发生任何变化。我知道这一点,这就是为什么我不理解它!但事实上,取消注释这一行会使内容正常工作。