{"id":625,"date":"2023-02-26T11:06:49","date_gmt":"2023-02-26T03:06:49","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=625"},"modified":"2023-04-29T20:16:52","modified_gmt":"2023-04-29T12:16:52","slug":"php-stream-context-create-simulate-get-post-requests","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/php-stream-context-create-simulate-get-post-requests\/","title":{"rendered":"PHP stream_context_create\u6a21\u62dfGET\/POST\u8bf7\u6c42"},"content":{"rendered":"<p>stream_context_create\u4f5c\u7528\uff1a<br \/>\n\u521b\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a\u6587\u672c\u6570\u636e\u6d41\u5e76\u5e94\u7528\u5404\u79cd\u9009\u9879\uff0c\u53ef\u7528\u4e8e<code>fopen()<\/code>,<code>file_get_contents()<\/code>\u7b49\u8fc7\u7a0b\u7684\u8d85\u65f6\u8bbe\u7f6e\u3001\u4ee3\u7406\u670d\u52a1\u5668\u3001\u8bf7\u6c42\u65b9\u5f0f\u3001\u5934\u4fe1\u606f\u8bbe\u7f6e\u7684\u7279\u6b8a\u8fc7\u7a0b\u3002<\/p>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/php.net\/manual\/zh\/function.stream-context-create.php\">http:\/\/php.net\/manual\/zh\/function.stream-context-create.php<\/a><\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-php\">function sign($value) {\n    $fp = fopen(&#039;pri_key.pem&#039;, &quot;r&quot;);\n    $pub_key_string = fread($fp, 8192);\n    $pkeyid = openssl_pkey_get_private($pub_key_string);\n    openssl_sign($value, $signature, $pkeyid);\n    return base64_encode($signature);\n}\n\n$data = &#039;test&#039;;\n$data_encode = base64_encode($data);\n$url = &#039;https:\/\/www.appblog.cn\/&#039; . $data_encode;\n\n$signature = sign(base64_encode($data));\n\n$opts = array (\n    &#039;http&#039; =&gt; array (\n        &#039;method&#039; =&gt; &quot;GET&quot;,\n        &#039;header&#039; =&gt; &quot;Content-Type: application\/json\\r\\n&quot; .\n                    &quot;signType: RSA\\r\\n&quot; .\n                    &quot;signature: &quot; . $signature . &quot;\\r\\n&quot;     \n    ),\n    &quot;ssl&quot; =&gt; array(\n        &quot;verify_peer&quot; =&gt; false,\n        &quot;verify_peer_name&quot; =&gt; false,\n    )\n);\n\n$context = stream_context_create($opts);\n$response = json_decode(file_get_contents($url, false, $context));\n\nprint_r($response);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>stream_context_create\u4f5c\u7528\uff1a \u521b\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a\u6587\u672c\u6570\u636e\u6d41\u5e76\u5e94\u7528\u5404\u79cd\u9009\u9879\uff0c\u53ef\u7528\u4e8efopen() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[129],"class_list":["post-625","post","type-post","status-publish","format-standard","hentry","category-php-basic","tag-http"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=625"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/625\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}