PHP stream_context_create模拟GET/POST请求

stream_context_create作用:
创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。

参考:http://php.net/manual/zh/function.stream-context-create.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function sign($value) {
$fp = fopen('pri_key.pem', "r");
$pub_key_string = fread($fp, 8192);
$pkeyid = openssl_pkey_get_private($pub_key_string);
openssl_sign($value, $signature, $pkeyid);
return base64_encode($signature);
}

$data = 'test';
$data_encode = base64_encode($data);
$url = 'https://www.appblog.cn/' . $data_encode;

$signature = sign(base64_encode($data));

$opts = array (
'http' => array (
'method' => "GET",
'header' => "Content-Type: application/json\r\n" .
"signType: RSA\r\n" .
"signature: " . $signature . "\r\n"
),
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
)
);

$context = stream_context_create($opts);
$response = json_decode(file_get_contents($url, false, $context));

print_r($response);

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :