Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.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能否将json数据发送回调用脚本,然后像flush()一样继续处理?_Php_Jquery - Fatal编程技术网

PHP能否将json数据发送回调用脚本,然后像flush()一样继续处理?

PHP能否将json数据发送回调用脚本,然后像flush()一样继续处理?,php,jquery,Php,Jquery,jQuery 或者我需要将写操作移出php并创建第二个ajax调用,将json传递给它吗?您需要使用ini_设置“display_errors”,“Off”来显示从显示开始的内容;在脚本的顶部。然后,您应该通过将以下内容放在顶部来记录所有错误: $file = $rootPath.'wp-content/uploads/test/'.$query.".txt"; if (file_exists($seoCacheFile) && is_readable($seoCacheFil

jQuery


或者我需要将写操作移出php并创建第二个ajax调用,将json传递给它吗?

您需要使用ini_设置“display_errors”,“Off”来显示从显示开始的内容;在脚本的顶部。然后,您应该通过将以下内容放在顶部来记录所有错误:

$file = $rootPath.'wp-content/uploads/test/'.$query.".txt";

if (file_exists($seoCacheFile) && is_readable($seoCacheFile))
{
    $retarr = file_get_contents($file);
    if($retarr !=="")
    {
        print_r($retarr);die;
    }
}


if(count($ret)){
    $retarr = json_encode(array('response' => array('test' => array('Results'=>$ret))));
    print_r($retarr);
    flush();

    //PROBLEM IS HERE
    //If an error occurs here, the json is mangled, flush() does not help. Can I send the json and deal with PHP errors separately?
    /*  if (!is_dir($rootPath.'wp-content/uploads/test/')){mkdir($rootPath.'wp-content/uploads/test/');}
        $seoCacheFile = fopen($rootPath.'wp-content/uploads/test/'.$query.".txt",'w');
        fwrite($File, $retarr);fclose($File);
    */
}
然后,您需要处理代码中可能出现的错误,如果遇到错误,请发回相应的信息。例如:

ini_set('log_errors','On');
ini_set('error_log','path-to-phperror-log.log');

另外,确保$query被正确转义。您不希望有人传入像../../../wp login.php这样的路径。

损坏的输出是什么样子的?您应该能够在一个请求中同时完成这两项工作,只要代码的其余部分不输出任何内容,即抑制任何其他错误,或者在出现问题时先写入文件并在JSON中发送错误通知。输出是完美的JSON数据,然后以PHP错误消息结束,这会丢弃调用脚本中的所有内容。您可以将“显示错误”设置为禁用,但您可能只需修复错误,这样它就不会损坏您的输出。打印字符串将起作用,因为对于字符串,它只打印字符串。这是一种奇怪的做事方式。
ini_set('log_errors','On');
ini_set('error_log','path-to-phperror-log.log');
json_encode(array('result' => 'error', 'error' => 'File I/O error.'));