服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - php采用file_get_contents代替使用curl实例

php采用file_get_contents代替使用curl实例

2020-08-06 15:43PHP教程网 PHP教程

这篇文章主要介绍了php采用file_get_contents代替使用curl的方法,实例讲述了file_get_contents模拟curl的post方法,对于服务器不支持curl的情况来说有一定的借鉴价值,需要的朋友可以参考下

本文实例讲述了php采用file_get_contents代替使用curl的方法,分享给大家供大家参考。具体实现方法如下:

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(
            'http' => array(
                'method' => 'POST',
                'header' =>"Content-type: application/x-www-form-urlencoded",
                'content' => $post
            )
        );
        return file_get_contents($url, false, stream_context_create($options));
}

 

希望本文所述对大家的php程序设计有所帮助。

延伸 · 阅读

精彩推荐