How does php retrieve valid URLs in the text?
I do a 24-hour random jump program, first of all, the URL is written into a txt document, one by one. Then use php to read the URL in the txt, take out the accessible URL, skip if there is an inaccessible URL, rule it out and continue to select!
the current method is relatively simple to implement loop acquisition
<?php
//txt
$data=file_get_contents("test.txt");
//
$data_url=explode("\r\n",$data);
//
$data_num = rand(0, count($data_url)-1);
//
$url = $data_url[$data_num];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
$curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
//
if ($curl_code == 200) {
//cookie
if (isset($_COOKIE["data"])){
$tzUrl=$_COOKIE["data"];
echo ":".$tzUrl;
//header ("Location: $tzUrl");
}else{
//
//header ("Location: $data_url[$data_num]");
$value = $data_url[$data_num];
//setcookie("data",$value, time()+3600/60/6);
//24
setcookie("data",$value, time()+3600*24);
};
//echo ":" . $curl_code;
}else {
//echo ":" . $curl_code;
}
the boss of the program will take a look at it, and you will be paid a reward
.
PHP: getallheaders-Manual- http://php.net/manual/zh/func.
this is a class library that encapsulates TXT files as a database to read and write TXT files, in which you can convert TXT text into an array, and then traverse the array to request a URL. Reference source code for you.
<?php
namespace model;
use think\Model;
Class SensitiveModel extends Model{
private static $txtpath;
private static $stop = FALSE;
private static $wrong;
//
public function name_check($path){
$name = substr($path,-4);
if($name == ".txt"){
return TRUE;
}else{
return FALSE;
}
}
//
public function open($path,$data=FALSE){
if(self::name_check($path)){
if(!file_exists($path)){
if($data){
$this->create($path,$data);
}else{
$this->create($path);
}
self::$txtpath = $path;
return TRUE;
}else{
self::$txtpath = $path;
return TRUE;
}
}else{
self::$stop = TRUE;
self::$wrong = "";
return FALSE;
}
}
//
public function read($echo = FALSE,$blank = FALSE){
if(!self::$stop && self::$txtpath){
$data = file_get_contents(self::$txtpath);
if($blank){
$data = str_replace("\n","<br />",$data);
}
if($echo){
echo $data;
}else{
return $data;
}
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "";
}
return FALSE;
}
}
//
public function createtxt($path,$data=FALSE){
if(self::name_check($path)){
if(file_exists($path)){
self::$stop = TRUE;
self::$wrong = "";
return FALSE;
}else{
file_put_contents($path,$data);
self::$txtpath = $path;
return TRUE;
}
}else{
self::$stop = TRUE;
self::$wrong = "";
return FALSE;
}
}
//
public function change($data){
if(!self::$stop && self::$txtpath){
file_put_contents(self::$txtpath,$data);
return TRUE;
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "";
}
return FALSE;
}
}
//
public function appendtxt($data,$line=FALSE){
if(!self::$stop && self::$txtpath){
if($line && $this->read()){
$data = "\r\n".$data;
}
file_put_contents(self::$txtpath,$data, FILE_APPEND);
return TRUE;
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "";
}
return FALSE;
}
}
//
public function delete(){
if(!self::$stop && self::$txtpath){
unlink(self::$txtpath);
return TRUE;
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "";
}
return FALSE;
}
}
//
public function wrong(){
if(self::$wrong){
return self::$wrong;
}
}
//
public function get_line($line_number){
if(!self::$stop && self::$txtpath){
$data = $this->read();
$lines = explode("\n",$data);
if(array_key_exists($line_number,$lines)){
return $lines[$line_number];
}else{
self::$stop = TRUE;
self::$wrong = "";
return FALSE;
}
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "";
}
return FALSE;
}
}
//
public function del_line($line_number){
if(!self::$stop && self::$txtpath){
$data = $this->read();
$lines = explode("\n",$data);
if(array_key_exists($line_number,$lines)){
unset($lines[$line_number]);
$newdata = implode("\n",$lines);
$this->change($newdata);
return TRUE;
}else{
self::$stop = TRUE;
self::$wrong = "";
return FALSE;
}
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "";
}
return FALSE;
}
}
//
public function change_line($line_number,$text){
if(!self::$stop && self::$txtpath){
$data = $this->read();
$lines = explode("\n",$data);
if(array_key_exists($line_number,$lines)){
$lines[$line_number] = $text;
$newdata = implode("\n",$lines);
$this->change($newdata);
return TRUE;
}else{
self::$stop = TRUE;
self::$wrong = "";
return FALSE;
}
}else{
if(!self::$wrong){
self::$stop = TRUE;
self::$wrong = "";
}
return FALSE;
}
}
//txtarr
public function txt2arr($path){
if(self::name_check($path)){
$data = array();
if(file_exists($path)){
$stream = file_get_contents($path);
$str_encoding = mb_convert_encoding($stream, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
$data = explode("\r\n", $str_encoding);
foreach ($data as &$row) {
$row = trim($row);
}
unset($row);
}
return $data;
}
}
//arrtxt,
public function arr2txt($path,$arr){
$arr = implode("\r\n",$arr);
if($this->name_check($path)){
$res = file_put_contents($path,$arr);
if($res){
return true;
}else{
return false;
}
}else{
return false;
}
}
//arrtxt,
public function arrappendtxt($path,$arr){
if($this->name_check($path)){
$arr = implode("\r\n",$arr);
$str = "\r\n";
file_put_contents($path,$str,FILE_APPEND);
$res = file_put_contents($path,$arr,FILE_APPEND);
if($res){
return true;
}else{
return false;
}
}else{
return false;
}
}
//PHP
public function pagenate($arr,$page=1,$size=10){
//,
$pages = ceil(count($arr)/$size);
$newarr = array_slice($arr,($pages-1)*$size,$size);
for($i=0;$i<count($newarr);$iPP){
$data[$i] = $arr[$i];
}
return $data;
}
}
the change of the landlord should be able to use it.
add it on your basis. I hope it will be useful
<?php
//txt
$data = file_get_contents("test.txt");
//windows\r\nlinux\nPHP
$data_url = explode(PHP_EOL, $data);
//
$usefulWebsite = [];
//
while (count($data_url)) {
//
$data_num = rand(0, count($data_url) - 1);
//
$url = $data_url[$data_num];
// unsetunsetkey
array_splice($data_url, $data_num, 1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
$curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
//
if ($curl_code == 200) {
//
$usefulWebsite[] = $url;
//cookie
if (isset($_COOKIE["data"])) {
$tzUrl = $_COOKIE["data"];
echo ':' . $tzUrl;
//header ("Location: $tzUrl");
} else {
//
//header ("Location: $url");
$value = $url;
//setcookie("data",$value, time()+3600/60/6);
//24
setcookie("data", $value, time() + 3600 * 24);
};
//echo ':' . $curl_code;
} else {
//echo ':' . $curl_code;
//
continue;
}
}
print_r($usefulWebsite);