关于php
Posted 梁大师
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于php相关的知识,希望对你有一定的参考价值。
php DOMDocument可以操作xml
1 <?php 2 3 //定义参数 4 $data = array(‘sParam1‘ => ‘test1‘, ‘sParam2‘ => 101, ‘isAuto‘ => 1); 5 6 //把参数转换成URL数据 7 $data = @http_build_query($data); 8 9 $aContext = array( 10 ‘http‘ => array( 11 ‘method‘ => ‘GET‘, 12 13 ‘header‘ => ‘Content-type: application/x-www-form-urlencoded‘, 14 15 ‘content‘ => $data, 16 ), 17 ); 18 19 $cxContext = stream_context_create($aContext); 20 21 //此处必须为完整路径 22 $sUrl = ‘http://www.mytest.com/test.php‘; 23 24 $d = @file_get_contents($sUrl, false, $cxContext); 25 26 print_r($d); 27 28 ?>
1 <?php 2 3 $data = array(‘sParam1‘ => ‘test1‘, ‘sParam2‘ => 101, ‘isAuto‘ => 1); //定义参数 4 5 $data = @http_build_query($data); //把参数转换成URL数据 6 7 $aContext = array( 8 ‘http‘ => array( 9 ‘method‘ => ‘POST‘, 10 11 ‘header‘ => ‘Content-type: application/x-www-form-urlencoded‘, 12 13 ‘content‘ => $data, 14 ), 15 ); 16 17 $cxContext = stream_context_create($aContext); 18 19 $sUrl = ‘http://www.mytest.com/test.php‘; //此处必须为完整路径 20 21 $d = @file_get_contents($sUrl, false, $cxContext); 22 23 print_r($d); 24 25 ?>
1 <?php 2 /** 3 * curl file_get_content fsocket 来实现post提交数据 4 */ 5 class Request { 6 7 public static function post($url, $post_data = ‘‘, $timeout = 5) { 8 //curl 9 $ch = curl_init(); 10 curl_setopt($ch, CURLOPT_URL, $url); 11 curl_setopt($ch, CURLOPT_POST, 1); 12 if ($post_data != ‘‘) { 13 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 14 } 15 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 16 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 17 curl_setopt($ch, CURLOPT_HEADER, false); 18 $file_contents = curl_exec($ch); 19 curl_close($ch); 20 return $file_contents; 21 } 22 23 public static function post2($url, $data) { 24 //file_get_content 25 $postdata = http_build_query($data); 26 27 $opts = array( 28 ‘http‘ => array( 29 ‘method‘ => ‘POST‘, 30 ‘header‘ => ‘Content-type: application/x-www-form-urlencoded‘, 31 ‘content‘ => $postdata, 32 ), 33 34 ); 35 $context = stream_context_create($opts); 36 $result = file_get_contents($url, false, $context); 37 return $result; 38 39 } 40 41 public static function post3($host, $path, $query, $others = ‘‘) { 42 //fsocket 43 $post = "POST $path HTTP/1.1\r\nHost: $host\r\n"; 44 $post .= "Content-type: application/x-www-form-"; 45 $post .= "urlencoded\r\n${others}"; 46 $post .= "User-Agent: Mozilla 4.0\r\nContent-length: "; 47 $post .= strlen($query) . "\r\nConnection: close\r\n\r\n$query"; 48 $h = fsockopen($host, 80); 49 fwrite($h, $post); 50 for ($a = 0, $r = ‘‘;!$a;) { 51 $b = fread($h, 8192); 52 $r .= $b; 53 $a = (($b == ‘‘) ? 1 : 0); 54 } 55 fclose($h); 56 return $r; 57 } 58 }
以上是关于关于php的主要内容,如果未能解决你的问题,请参考以下文章