php - 需要输出到 txt 文件而不是 json 对象
问题描述
此代码是否可转换以将其结果打印到 txt 文件,而不是 JSON / curl 对象?我们正在尝试调试特定的 sku,我们希望查看此代码段之前的长查询的输出:
foreach($group_sets AS $group_set) {
$bulk_json .= '{ "index" : { "_id" : "'.$group_set['our_sku'].'" } }'.PHP_EOL;
$bulk_json .= json_encode($group_set).PHP_EOL;
}
foreach($remove_skus AS $sku) {
$bulk_json .= '{ "delete" : { "_id" : "'.$sku.'" } }'.PHP_EOL;
}
print "processing batch, batch count: ".$batch_cnt.PHP_EOL;
send_to_elastic($bulk_json);
$bulk_json = "";
$batch_cnt = 0;
$batch_sku_list = array();
}
}
if(!empty($bulk_json)) {
send_to_elastic($bulk_json);
$bulk_json = "";
}
print PHP_EOL.PHP_EOL."DONE".PHP_EOL.PHP_EOL;
function send_to_elastic($bulk_json) {
$url = "https://ada64ff1913a4b.us-east-1.aws.found.io:9243/us/product/_bulk";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, "MyName:MyPassword");
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $bulk_json);
echo "uploading batch to elastic-cloud... ";
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$response = json_decode($json_response, true);
//if ( $status != 201 ) {
// print("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
//}
curl_close($curl);
echo "done".PHP_EOL;
这是我修改后的代码,虽然它没有写入文件,但很明显我搞砸了......
foreach($group_sets AS $group_set) {
$bulk_json .= '{ "index" : { "_id" : "'.$group_set['my_sku'].'" } }'.PHP_EOL;
$bulk_json .= json_encode($group_set).PHP_EOL;
}
foreach($remove_skus AS $sku) {
$bulk_json .= '{ "delete" : { "_id" : "'.$sku.'" } }'.PHP_EOL;
}
print "processing batch, batch count: ".$batch_cnt.PHP_EOL;
//send_to_elastic($bulk_json);
file_put_contents('searchresults.txt', $bulk_json);
$bulk_json = "";
$batch_cnt = 0;
$batch_sku_list = array();
}
}
if(!empty($bulk_json)) {
//send_to_elastic($bulk_json);
file_put_contents('searchresults.txt', $bulk_json);
$bulk_json = "";
}
print PHP_EOL.PHP_EOL."DONE".PHP_EOL.PHP_EOL;
//function send_to_elastic($bulk_json) {
//$url = "https://ada64ff1913a4b16us-east-1.aws.found.io:9243/qm/product/_bulk";
//$curl = curl_init($url);
//curl_setopt($curl, CURLOPT_HEADER, false);
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_USERPWD, "MyUser:MyPW");
//curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
//curl_setopt($curl, CURLOPT_POST, true);
//curl_setopt($curl, CURLOPT_POSTFIELDS, $bulk_json);
//echo "uploading batch to elastic-cloud... ";
//$json_response = curl_exec($curl);
//$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
//$response = json_decode($json_response, true);
//if ( $status != 201 ) {
// print("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
//}
//curl_close($curl);
echo "done".PHP_EOL;
解决方案
使用file_put_contents()
. 将调用替换为send_to_elastic()
:
file_put_contents('filename.txt', $bulk_json);
您还应该正确生成 JSON,例如:
$result = array();
foreach($group_sets AS $group_set) {
$result[] = ["index" => [ "_id" => $group_set['our_sku']]];
$result[] = $group_set;
}
foreach($remove_skus AS $sku) {
$result[] = [ "delete" => [ "_id" => $sku ]];
}
$bulk_json = json_encode($result);
推荐阅读
- angular - 如何在角度 6 中推送具有两个参数的对象?
- swift - WKNavigationResponse 不再通过其 response.header 提供对 cookie 的访问
- javascript - Python 使用两个参数运行 JavaScript 函数
- java - 依赖注入和静态方法
- react-native - 使用 expo 发布 React Native 应用程序
- python - 优化测试 API 结果的 python 代码的速度
- javascript - Vue.js 在页面刷新时返回 JSON
- go - go中的函数数组语法是什么意思?
- mysql - 计算从内容构建到内容构建过程中丢失和获得的记录
- oracle - 在弹出警报中而不是在状态栏上显示错误消息