需要输出到txt文件而不是json对象

时间:2018-12-14 19:48:34

标签: php json curl

此代码是否可转换以将其结果打印到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;

1 个答案:

答案 0 :(得分:0)

使用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);