【问题标题】:need output to txt file instead of json object需要输出到 txt 文件而不是 json 对象
【发布时间】:2018-12-14 19:48:34
【问题描述】:

此代码是否可转换以将其结果打印到 txt 文件,而不是 JSON / curl 对象?我们正在尝试调试一个特定的 sku,我们希望看到这个 sn-p 之前的长查询的输出:

     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;

【问题讨论】:

  • 永远不要使用这样的字符串操作来构建 JSON。创建一个数组并使用json_encode()
  • 当然,您的问题的答案是:可以修改代码以写入文本文件而不是发送到弹性文件。但我不知何故怀疑这是你真正的问题......
  • 这样转换有什么问题?使用file_put_contents() 将字符串写入文件。
  • $bulk_json 不是有效的 JSON。所有对象都需要是数组的元素,并且需要用逗号分隔。
  • 是的,我想我应该更具体地说明我想在这里得到什么,我的第一篇文章,请原谅新手

标签: php json curl


【解决方案1】:

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

【讨论】:

  • 谢谢,但我究竟应该在哪里添加它,我会注释掉什么,这样它就不会发送到它现在所做的事情?
  • 你这样做不是很明显,而不是打电话给send_to_elastic()吗?
  • 我如何在这里发布我修改后的代码是什么样的?添加评论对此不起作用。
  • 编辑问题并在底部添加新代码。不要删除原始代码。
  • 我添加了修改后的代码。顺便说一句,searchresults.txt 的权限为 777,但没有写入任何内容。
猜你喜欢
  • 2014-08-06
  • 2016-03-29
  • 1970-01-01
  • 2018-07-31
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多