20 |
while ( $str = trim( fgets ( $socket , 4096))) {
|
25 |
while (! feof ( $socket )) {
|
26 |
$data .= fgets ( $socket , 4096);
|
Curl版本:
04 |
* $post_string = "app=request&version=beta";
|
05 |
* request_by_curl('http://blog.snsgou.com/restServer.php', $post_string);
|
07 |
function request_by_curl( $remote_server ,
$post_string ) {
|
09 |
curl_setopt( $ch , CURLOPT_URL,
$remote_server );
|
10 |
curl_setopt( $ch , CURLOPT_POSTFIELDS,
'mypost=' . $post_string );
|
11 |
curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true);
|
12 |
curl_setopt( $ch , CURLOPT_USERAGENT,
"snsgou.com's CURL Example beta" );
|
13 |
$data = curl_exec( $ch );
|
Curl版本(2)
04 |
* @param string $url request address
|
05 |
* @param string $method Request method GET/POST
|
* @param string $refererUrl request source address
|
07
|
* @param array $data Send data
|
08
|
* @param string $contentType
|
09
|
* @param string $timeout
|
10
|
* @param string $proxy
$url,
$data
, $refererUrl |
= '' , $method = 'GET' ,
$contentType = 'application/json' ,
$timeout = 30, $proxy = false) {
14
| $ch = null;
('POST'
===
strtoupper ( |
$method )) {
16
|
17
$ch
, CURLOPT_POST, 1);
|
18
|
, CURLOPT_HEADER,0 );
~ |
20
ch
| , CURLOPT_RETURNTRANSFER, 1);
| 21
$ch , CURLOPT_FORBID_REUSE, 1);
|
22
|
curl_setopt( $ch , CURLOPT_TIMEOUT,
$timeout ); |
25
27 |
curl_setopt( $ch , CURLOPT_HTTPHEADER,
array ( 'Content-Type:' . $contentType ));
|
30 |
curl_setopt( $ch , CURLOPT_POSTFIELDS,
$data );
|
32 |
curl_setopt( $ch , CURLOPT_POSTFIELDS, http_build_query( $data ));
|
34 |
} else if ( 'GET' === strtoupper ( $method )) {
|
35 |
if ( is_string ( $data )) {
|
36 |
$real_url = $url . ( strpos ( $url ,
'?' ) === false ? '?' : '' ).
$data ;
|
38 |
$real_url = $url . ( strpos ( $url ,
'?' ) === false ? '?' : '' ). http_build_query( $data );
|
41 |
$ch = curl_init( $real_url );
|
42 |
curl_setopt( $ch , CURLOPT_HEADER, 0);
|
43 |
curl_setopt( $ch , CURLOPT_HTTPHEADER,
array ( 'Content-Type:' . $contentType ));
|
44 |
curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1);
|
45 |
curl_setopt( $ch , CURLOPT_TIMEOUT,
$timeout );
|
47 |
curl_setopt( $ch , CURLOPT_REFERER,
$refererUrl );
|
50 |
$args = func_get_args();
|
55 |
curl_setopt( $ch , CURLOPT_PROXY,
$proxy );
|
58 |
$info = curl_getinfo( $ch );
|
调用 WCF接口 的一个例子:$json = restRequest($r_url,'POST', json_encode($data));
以上就介绍了php实现post和get,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
|