粗略写了一个导码功能,估计还好烂,值得改进
<?php
ini_set('memory_limit', '-1');//不要限制Mem大小,否则会报错
//succ.txt
//duplicate.txt
//fail.txt
//输出文本中所有的行,直到文件结束为止。
$file = "MA.txt";//100W条记录的TXT源文件
$lines = file_get_contents($file);
ini_set('memory_limit', '-1');//不要限制Mem大小,否则会报错
$line=explode("\n",$lines);
//1200001
echo date("Y-m-d H:i:s",time());
$array = [];
//10230
for($i=9000;$i<10231;$i++){
$y = $i*100;
$code = array_slice($line, $y, 100);
file_put_contents("test.txt", date("Y-m-d H:i:s",time())."第".$y." 行数 ".json_encode($code).PHP_EOL, FILE_APPEND);
$url = "http://api.weixin.qq.com/card/code/deposit?access_token=";
$post_data = array(
"card_id" => "",
"code" => $code
);
$result_json = request_post($url, $post_data);
$result = json_decode($result_json,true);
if($result["errcode"] == 0 && $result["errmsg"] == "ok"){
file_put_contents("succ.txt", date("Y-m-d H:i:s",time())."第".$y." 行数 ".json_encode($result["succ_code"]).PHP_EOL, FILE_APPEND);
file_put_contents("duplicate.txt", date("Y-m-d H:i:s",time())."第".$y." 行数 ".json_encode($result["duplicate_code"]).PHP_EOL, FILE_APPEND);
file_put_contents("fail.txt", date("Y-m-d H:i:s",time())."第".$y." 行数 ".json_encode($result["fail_code"]).PHP_EOL, FILE_APPEND);
}else{
file_put_contents("fail.txt", date("Y-m-d H:i:s",time())."第".$y." 行数 ".json_encode($code).PHP_EOL, FILE_APPEND);
}
}
echo date("Y-m-d H:i:s",time());
/*微信接口调用
*/
function request_post($url = '', $post_data = array()) {
if (empty($url) || empty($post_data)) {
return false;
}
$data = json_encode($post_data);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
$return = curl_exec ( $ch );
curl_close ( $ch );
return $return;
}
?>