文件输出(用于输出log):
file_put_contents(filePath, printLog, FILE_APPEND );跨域处理 :
header(‘Access-Control-Allow-Credentials:true’);
header(‘content-type:application/json;charset=utf8’);指定允许其他域名访问:
header(‘Access-Control-Allow-Origin:*’);响应类型:
header(‘Access-Control-Allow-Methods:POST’);响应头设置:
header(‘Access-Control-Allow-Headers:content-type’);获取当前时间:
date(‘y-m-d h:i:s’,time());数组转换成字符串:
implode();字符串转化为整数:
(int)$foo; intval($foo);字符串切割成数组,如果是中文,utf-8下应是按3个字节切割,否则为乱码:
str_split($str,3)返回字符串长度,如果是中文,utf-8下一个中文返回3个字节长度:
strlen($str)返回字符串长度,如果是中文,默认UTF-8编码,返回一个字节长度:
mb_strlen($str,’utf-8’)字符串截取 如果 start 是负数,返回的字符串将从结尾处向前数第 start 个字符开始:
substr($str,2,2)截取中文字符串,一个中文按一个字节处理:
mb_substr($str,2,2,’utf-8’)计算字串出现的次数,区分大小写:
substr_count($str,’h’)计算字串出现的次数,区分大小写:
mb_substr_count($str,’中’)首字母大写:
ucfirst($str)将字符串中每个单词的首字母转换为大写:
ucwords($str)将字符串转化为小写:
strtolower($str)将字符串转化为大写:
strtoupper($str)字符串翻转,如果含有中文,将会乱码:
strrev($str)字串替换,在string中查找第一个参数,替换成第二个参数:
str_replace (search,replace,$str)随机打乱一个字符串,中文会乱码:
str_shuffle ($str)查找字符串的首次出现 ,第三个参数表示返回是出现前还是出现后,默认false:
strstr($str,’wo’,true)重复一个字符串,返回值为重复这个字符串10次的值:
str_repeat(“-=”, 10)字符串转为时间:
strtotime (date(“y-m-d h:i:s”)); //当前时间 ,注意H 是24小时 h是12小时PHP发送http请求:
用 file_get_contents 以get方式获取内容:
1
2$url = 'https://wenda.shukaiming.com/';
echo file_get_contents($url);用 file_get_contents函数,以post方式获取url
1
2
3
4
5
6$data = array(‘foo' => ‘bar');
$data = http_build_query($data);
$opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencodedrn' . 'Content-Length: ' . strlen($data) . '\r\n', 'content' => $data));
$context = stream_context_create($opts);
$html = file_get_contents('https://wenda.shukaming.com', false, $context);
echo $html;通过CURL发送get请求
1
2
3
4
5
6
7$ch=curl_init('http://www.xxx.com/xx.html');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
$output=curl_exec($ch);
$fh=fopen("out.html",'w');
fwrite($fh,$output);
fclose($fh);通过fsocket发送get请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {
$socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
if (!$socket) die("$errstr($errno)");
fwrite($socket, "POST $remote_path HTTP/1.0");
fwrite($socket, "User-Agent: Socket Example");
fwrite($socket, "HOST: $remote_server");
fwrite($socket, "Content-type: application/x-www-form-urlencoded");
fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . "");
fwrite($socket, "Accept:*/*");
fwrite($socket, "");
fwrite($socket, "mypost=$post_string");
fwrite($socket, "");
$header = "";
while ($str = trim(fgets($socket, 4096))) {
$header .= $str;
}
$data = "";
while (!feof($socket)) {
$data .= fgets($socket, 4096);
}
return $data;
}
最后更新: 2020年11月27日 09:26
原始链接: http://genpe.top/2020/11/27/PHP/PHP%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95/