Blogger Information
Blog 14
fans 0
comment 0
visits 11550
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【php】while/for循环,网络请求
暮光薄凉
Original
644 people have browsed it
1.while循环
  1. while(判断条件){
  2. 代码块
  3. }
  4. do{
  5. 代码块
  6. }while(判断条件)
2.for循环
  1. for(条件1;条件2;条件3){
  2. 代码块
  3. }
3.随机函数
  1. mt_rand(0,9);
  2. $a = '123456asdfghjkl';
  3. //获取字符串长度
  4. strlem($a);
  5. //获取随机字符
  6. $a[mt_rand(0,strlem($a)-1)];
4.全局变量、常量
变量 注释
post: 密文传输
gst 明文传输
$_REQUEST: 一维数组,包含$_get/$_post/$_COOIKE
$_GLOBALS: 二维数组,包含$_get/$_post/$_COOIKE/$_FILES
$_SERVER 获取服务器环境信息
$_COOKIE 客户端缓存信息
$_SESSION 服务器器缓存信息
$_FILES 文件上传信息
预定义常量 注释
FILE 当前文件
DIR 当前目录
5.网络请求
  1. file(); 把整个文件读入到一个数组中
  2. 例:file('http://www.php.cn/');
  3. file_get_contents(); 读取到的数据是一个字符串
  1. curl网络请求
  2. $cs = curl_init(); //创建curl
  3. //配置请求参数
  4. curl_setopt($cs,CURLOPT_URL,'http://apis.juhe.cn');
  5. $data = [
  6. 'key' => '123456',
  7. 'city' => '北京'
  8. ];
  9. curl_setopt($cs,CURLOPT_POST,1);
  10. curl_setopt($cs,CURLOPT_POSTFIELDS,$data);
  11. //执行请求
  12. $s = curl_exec($cs);
  13. /*****简化封装******/
  14. function get_url($url,$data,$is_post=0){
  15. $cs = curl_init();
  16. if($is_post== 0){
  17. if(!empty($data)){
  18. $url .= '?';
  19. foreach($data as $k=>$v){
  20. $url .= $k.'='.$v.'&';
  21. }
  22. }
  23. }
  24. curl_setopt($cs,CURLOPT_URL,$url);
  25. if($is_post== 1){
  26. curl_setopt($cs,CURLOPT_POST,1);
  27. curl_setopt($cs,CURLOPT_POSTFIELDS,$data);
  28. }
  29. $s = curl_exec($cs);
  30. curl_close($cs);//关闭请求,不能被打印
  31. return $s;
  32. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post