file_get_contents版本:
01 |
| 02
|
| 03
|
* @param string $url 請求位址
|
|
*
|
|
|
| */
|
函數 | send_post(
函數
$post_data |
|
09
10
|
$post_data
|
|
12
|
數組
|
'方法' => '發表' ,
|
|
'內容類型:application/x-www-form-urlencoded' ,
|
|
16
| 'timeout'
|
)
|
|
$context =stream_context_create( $選項 );
|
20 |
$ ,
$上下文 );
|
| 22
| 回傳
|
23
|
23 |
} 23
} 23
|
23 |
}
1
$post_data = 數組 ( |
2 |
|
3
| '密碼'
=> | '邯郜4
|
send_post( |
'http://blog.snsgou.com' ,
$post_data );
|
/** |
|
| 03
| * 使用方法:
| 04
| * $post_string = "app=socket&version=beta";
|
|
|
06
|
|
07 _
| ,
$port | = 80,
$timeout = 30) {
= 30) {
$socket = fsockopen ( $remote_server |
,
,
$errstr |
, $timeout );
if (! $socket )
die ( "$errstr($errno)" );
|
$socket
"POST $remote_path HTTP/1.0" ); |
$socket ,
"User-Agent: Socket Example" );
|
,
"HOST: $remote_server" |
);
$socket
| ,
| fwrite(
$socket ,
"Content-length: " . ( strlen (
| $post_string
|
15com ,
"Accept:*/*" );
|
socket ,
|
"" );
|
,
"mypost=$post_string" |
);
$socket ,
"" );
|
""
|
20 |
while ( $插座 ,4096))){
|
|
22
}
|
23 |
|
""
|
25
while |
(! feof
|
.= fgets |
( $socket , 4096);
2 } |
28 |
$資料 ;
|
30 |
}
|
Curl版本:
*
|
函數 |
request_by_curl( $remote_server ,
$post_string ) { |
); |
|
09
curl_setopt(
, CURLOPT_URL,
$remote_server |
);
|
108 $ch 、CURLOPT_POSTFIELDS、
|
'mypost=' 。 $post_string |
|
11 ch ,CURLOPT_RETURNTRANSFER,true);
|
12 |
curl_setopt(
| $ch
, CURLOPT_USERAGENT,
);
|
|
);
|
14 |
|
15
|
17
|
}
|
|
01
/** * 發送HTTP請求
|
03 |
*
|
|
05
|
|
* @param string $refererUrl 請求來源位址
|
|
*param |
08
* @param string $contentType
|
|
09
*param |
|
|
|
| * @return
* @return |
12 |
*/
|
13 |
function send_request(
| $url
= | ''
, $method
$contentType = 'application/json' ,
$timeout = 30, $proxy = false) {
$ch = null;
15
|
|
| 16
|
17
curl_setopt( $ch , CURLOPT_POST, 1);
|
18
, CURLOPT_HEADER,0 );
|
19
|
$ch
|
20
ch , CURLOPT_RETURNTRANSFER, 1); |
( $ch |
, CURLOPT_FORBID_REUSE, 1);
|
curl_setopt( $ch |
curl_setopt( $ch , CURPT_TIME
$timeout ); |
|
$refererUrl ) {
|
24
curl_setopt( |
$ch , CURLOPT_ER, CURLOPT_ER,
$refererUrl );
|
|
26
( $contentType |
27 |
curl_setoptopv.
數組 ( 'Content-Type:' . $contentType ));
|
|
29
|
)){
|
30
curl_setopt( $ch , CURLOPT_POSTFIELDS,
|
|
31 {
|
curl_setopt( $ch |
, CURLOPT_POSTFIELDS, http_30_query();
33 |
}
|
34
} == '否則請問如果如果取得真toupper |
)) {
|
35 |
$資料
|
36
$real_url = $url$real_url = $url $real_url。 ( |
$url
'? ' ) === 假? |
'? ' : '' )。
$資料 ;
|
{
|
38
38
$real_url = $url 。 ( strpos ( $url ,
|
'? '
'' )。 http_build_query( |
$data );
|
|
40
41
$ch _$
42
|
|
curl_setopt( $ch |
(
. $contentType |
)); |
, CURLOPT_RETURNTRANSFER, 1); |
( $ch
| ,CURLOPT_TIMEOUT,
|
$refererUrl ) {
|
47 |
.
$refererUrl );
|
|
49
|
}否則
| {
|
|
51
|
假;
|
52
|
|
| 54
$代理 |
5 5
curl_setopt( |
$ch , CURLOPT_PROXY,
$代理 );
|
|
57
$ret =curl_exec( $ch |
| =curl_getinfo (
$ch );
|
59 |
$內容 =
|
'httpInfo'
|
=> 。 '發送' => $資料 , |
|
|
|
63
$ret , |
| 'http' => $訊息 ,
|
65 |
)
| );
| 67
68
|
|
回 $ret ;
|
} | 70
} restRequest($r_url,' POST', json_encode( $data)); |