wordpress rest api 发送空 json 文件
wordpress rest api send empty json file
打扰一下 :: 我的英文写作很糟糕 :))
我正在开发一个 wordpress 插件,我需要通过 rest api 和 excel 格式从自定义 table 中获取一些行。
通过此代码,excel 文件在 wordpress root 中创建并查询有效,但我无法在请求地址时下载它,
请求地址时,下载的excell文件是空的,里面只写了"resourse ... "
请帮助我,谢谢
add_action( 'rest_api_init', 'wpc_register_wp_api_endpoints' );
function wpc_register_wp_api_endpoints() {
register_rest_route( 'api', '/output/', array(
'methods' => 'GET',
'callback' => 'wpc_somename_search_callback',
'args' => array(
'date' => array(
'required' => true,
)
)
));
}
function wpc_somename_search_callback( $request ) {
global $wpdb;
$date = $request['date'];
$query = "SELECT * FROM {$wpdb->prefix}tickets where inroute_id=16 and date='$date'";
$list = $wpdb->get_results($query);
$jsonDecoded = json_decode( json_encode($list), true);
$fileName = 'example.csv';
$fp = fopen($fileName, 'w');
foreach($jsonDecoded as $row){
fputcsv($fp, $row);
}
fclose($fp);
header('Content-disposition: attachment; filename='.$fileName);
header('Content-type: application/json');
print $fp;
exit;
}
我的要求是
http://yootaabmarine.ir/wp-json/api/output/?date=13970822
我认为 $jsonDecoded = json_decode( json_encode($list), true);
在你的代码中没有用,你正在将数组转换为 JSON 字符串,然后将其转换回数组,最好使用 $list
直接。
$fp
包含一个资源,它本身是一个 PHP 类型(参见文档 fopen 函数 return 类型是 resource
) , 并且资源与字符串完全不同,所以你不能使用 print $fp;
.
我们可以使用readfile将文件正确发送到浏览器,这里是一个解决方案:
function wpc_somename_search_callback( $request ) {
global $wpdb;
$date = $request['date'];
$query = "SELECT * FROM {$wpdb->prefix}tickets where inroute_id=16 and date='$date'";
$list = $wpdb->get_results($query);
$fileName = 'example.csv';
$fp = fopen($fileName, 'w');
foreach($list as $row){
fputcsv($fp, $row);
}
fclose($fp);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'. $fileName .'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fileName));
readfile($fileName);
exit;
}
注意 SQL 注入,请改用 prepared queries。
打扰一下 :: 我的英文写作很糟糕 :)) 我正在开发一个 wordpress 插件,我需要通过 rest api 和 excel 格式从自定义 table 中获取一些行。 通过此代码,excel 文件在 wordpress root 中创建并查询有效,但我无法在请求地址时下载它, 请求地址时,下载的excell文件是空的,里面只写了"resourse ... " 请帮助我,谢谢
add_action( 'rest_api_init', 'wpc_register_wp_api_endpoints' );
function wpc_register_wp_api_endpoints() {
register_rest_route( 'api', '/output/', array(
'methods' => 'GET',
'callback' => 'wpc_somename_search_callback',
'args' => array(
'date' => array(
'required' => true,
)
)
));
}
function wpc_somename_search_callback( $request ) {
global $wpdb;
$date = $request['date'];
$query = "SELECT * FROM {$wpdb->prefix}tickets where inroute_id=16 and date='$date'";
$list = $wpdb->get_results($query);
$jsonDecoded = json_decode( json_encode($list), true);
$fileName = 'example.csv';
$fp = fopen($fileName, 'w');
foreach($jsonDecoded as $row){
fputcsv($fp, $row);
}
fclose($fp);
header('Content-disposition: attachment; filename='.$fileName);
header('Content-type: application/json');
print $fp;
exit;
}
我的要求是
http://yootaabmarine.ir/wp-json/api/output/?date=13970822
我认为 $jsonDecoded = json_decode( json_encode($list), true);
在你的代码中没有用,你正在将数组转换为 JSON 字符串,然后将其转换回数组,最好使用 $list
直接。
$fp
包含一个资源,它本身是一个 PHP 类型(参见文档 fopen 函数 return 类型是 resource
) , 并且资源与字符串完全不同,所以你不能使用 print $fp;
.
我们可以使用readfile将文件正确发送到浏览器,这里是一个解决方案:
function wpc_somename_search_callback( $request ) {
global $wpdb;
$date = $request['date'];
$query = "SELECT * FROM {$wpdb->prefix}tickets where inroute_id=16 and date='$date'";
$list = $wpdb->get_results($query);
$fileName = 'example.csv';
$fp = fopen($fileName, 'w');
foreach($list as $row){
fputcsv($fp, $row);
}
fclose($fp);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'. $fileName .'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fileName));
readfile($fileName);
exit;
}
注意 SQL 注入,请改用 prepared queries。