file_get_contents代替使用curl其實不多見了,但有時你碰到服務器不支持curl時我們可以使用file_get_contents代替使用curl了,下面看個例子.
當用盡一切辦法發現 服務器真的無法使用curl時,或者curl不支持https時,curl https 出現502時,你又不想重裝網站環境.
那你就改用file_get_contents 代替吧,curl 經常使用的 curl get curl post,curl get 替代 直接用file_get_contents($url) 就可以了.
curl post 替代,代碼如下:
- function Post($url, $post = null) {
- $content = http_build_query($post);
- $content_length = strlen($content);
- $options = array( //開源軟件:Vevb.com
- 'http' => array(
- 'method' => 'POST',
- 'header' =>"Content-type: application/x-www-form-urlencoded",
- 'content' => $post
- )
- );
- return file_get_contents($url, false, stream_context_create($options));
- }
新聞熱點
疑難解答