首页 > 编程知识 正文

php使用curl发送请求,php curl 上传文件

时间:2023-12-27 22:27:25 阅读:326035 作者:EXGV

本文目录一览:

PHP_CURL使用详解

CURL是PHP的一个扩展,利用该扩展可以实现服务器之间的数据或文件传输,用来采集网络中的html网页文件、其他服务器提供接口数据等。

GET请求

POST请求

php Curl的get和post方法

get方法

function http_get($url)

{

    $oCurl = curl_init();

    if (stripos($url, "https://") !== FALSE) {

        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);

        //curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);

        //CURL_SSLVERSION_TLSv1

    }

    curl_setopt($oCurl, CURLOPT_URL, $url);

    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);

    $sContent = curl_exec($oCurl);

    $aStatus = curl_getinfo($oCurl);

    curl_close($oCurl);

    if (intval($aStatus["http_code"]) == 200) {

        return $sContent;

    } else {

        return false;

    }

}

post方法

    // curlpost请求

    function http_post($url, $data = NULL, $json = false)

    {

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

        if (!empty($data)) {

            if ($json is_array($data)) {

                $data = json_encode($data);

            }

            curl_setopt($curl, CURLOPT_POST, 1);

            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

            if ($json) { //发送JSON数据

                curl_setopt($curl, CURLOPT_HEADER, 0);

                curl_setopt(

                    $curl,

                    CURLOPT_HTTPHEADER,

                    array(

                        'Content-Type: application/json; charset=utf-8',

                        'Content-Length:' . strlen($data)

                    )

                );

            }

        }

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $res = curl_exec($curl);

        $errorno = curl_errno($curl);

        if ($errorno) {

            return array('errorno' = false, 'errmsg' = $errorno);

        }

        curl_close($curl);

        return json_decode($res, true);

    }

php怎么响应客户端发送http请求

http请求有get,post。

php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]。

1. file_get_contents();详情见:

2. curl发送请求。

3. fsocket发送。

下面说使用curl发送。

首先环境需要配置好curl组件。

在windows中让php支持curl比较简单:

在php.ini中将extension=php_curl.dll前面的分号去掉,

有人说需要将php根目录的libeay32.dll和ssleay32.dll需要拷贝到系统目录下去。我实验不拷贝也可以。

在linux中,如果使用源码安装,需要在make 之前,./configure --with-curl=path,

其中,path是你的 libcurl库的位置,比如你安装libcurl库之后,

path可能就是/usr/local/,libcurl可以是静态库,也可以是动态库。

注意libcurl库configure的时候,可以将一些不需要的功能去掉,

比如ssl , ldap等。在php configure的时候,会去检查libcurl中某些功能是否被开启,进而去相应地调整生成的php。

如何利用php发送curl指令

$ch = curl_init(); // 初始化

curl_setopt($ch, CURLOPT_URL, "");

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 随301,302 自动跳转

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 不直接输出到页面

curl_setopt($ch, CURLOPT_TIMEOUTR, 5); // 设置超时时间

$content = curl_exec($ch); // 执行并返回内容

curl_close($ch); // 关闭

CURLOPT 选项很多,自己去php官网查看

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。