CURL访问举例

<?php
 function request($url, $params = [], $requestMethod = ‘GET‘, $jsonDecode = true, $headers = [])
    {
        $ci = curl_init();
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ci, CURLOPT_USERAGENT, ‘1001 Magazine v1‘);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ci, CURLOPT_TIMEOUT, 10);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ci, CURLOPT_ENCODING, "");
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ci, CURLOPT_HEADER, FALSE);

        $requestMethod = strtoupper($requestMethod);
        switch ($requestMethod) {
            case ‘POST‘:
                curl_setopt($ci, CURLOPT_POST, TRUE);
                if ($params) {
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
                } else {
                    curl_setopt($ci, CURLOPT_POSTFIELDS, ‘‘); // Don‘t know why: if not set,  413 Request Entity Too Large
                }
                break;
            case ‘DELETE‘:
                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, ‘DELETE‘);
                if ($params) {
                    $url = $url."?".$params;
                }
                break;
            case ‘GET‘:
                if ($params) {
                    $sep = false === strpos($url, ‘?‘) ? ‘?‘ : ‘&‘;
                    $url .= $sep . http_build_query($params);
                }
                break;
            case ‘PUT‘:
                if ($params) {
                    curl_setopt($ci, CURLOPT_CUSTOMREQUEST, "PUT");
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
                }
                break;
        }
        //$headers[] = "APIWWW: " . $_SERVER[‘REMOTE_ADDR‘];
        curl_setopt($ci, CURLOPT_URL, $url);
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);

        $response = curl_exec($ci);
        $httpCode = curl_getinfo($ci, CURLINFO_HTTP_CODE);
        $return = [
            ‘httpcode‘ => $httpCode,
            ‘data‘ => $httpCode >= 300 ? $response : ($jsonDecode ? json_decode($response, true) : $response),
            ‘response‘ => $response
        ];
        //$httpInfo = curl_getinfo($ci);
        curl_close($ci);
        return $return;
    }

    $res = request(‘http://www.che168.com/Handler/ScriptCarList_V1.ashx?needData=1‘,[],‘GET‘,false);

?>
时间: 2024-10-11 03:22:34

CURL访问举例的相关文章

php curl 访问web service接口

利用curl访问web service接口代码如下: $apiUrl = 'http://test.com/test'; $accessToken = '123456789'; $header = ['Authorization: ' . $accessToken,'Content-Type:application/json']; $post = ['sku'=>[]]; $jsonPost = json_encode($post); $ch = curl_init(); curl_setopt

用curl访问HTTPS站点并登录(对HTTP返回的结果特别清楚)

开发网站,少不了测试.现在的网站为了加强安全性,都启用了HTTPS协议.所谓HTTPS,也就是HTTP文本在SSL协议中传输.用curl命令行来测试HTTPS站点是个很有用的功能,写点脚本,就可以做功能测试. 假定Ubuntu系统运行着一个HTTPS站点,用CppCMS编写,Nginx配置了SSL证书,通过FastCGI和CppCMS编写的后台进程连接在一起. 第一步,安装: [plain] view plain copy print? apt-get install curl 我的Ubuntu

cURL 学习笔记与总结(5)用 cURL 访问 HTTPS 资源

<?php $curlobj = curl_init(); // 初始化 curl_setopt($curlobj, CURLOPT_URL, "https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js"); // 设置访问网页的URL curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true); // 执行之后不直接打印出来 // 设置HTTPS

php curl访问https 域名接口一直报错的问题

这两天一直在对接一个https的接口 通过本地postman完美链接后再服务器一直报错 出现问题:linux 下 curl可以正常访问 但是PHP请求一直返回false 测试方法:var_dump(curl_error($ch)); <在close之前> 返回:“SSL: certificate subject name 'xxx' does not match target host name 'www.miniscores.cn'” 这说明ssl证书有问题,如果对方没有给定证书.就忽略吧.

(004)Linux http命令curl访问url

curl www.baidu.com 访问一个网页 curl -o tt.html www.baidu.com 将访问www.baidu.com返回的内容写入tt.html curl -i www.baidu.com 显示响应的头信息 curl -v www.baidu.com 显示一次http请求的通信过程 curl -X GET/PUT/POST/DELETE url 执行GET/PUT/POST/DELETE操作(必须用大写) 原文地址:https://www.cnblogs.com/ja

PHP: 使用CURL访问FTP

今天要做FTP上传.本想用PHP自带的FTP函数来实现,结果发现这个模块没有编译进来,重新编译PHP太麻烦,改用其他方式实现吧  FTP上传 if (isset($_POST['Submit'])) { if (!empty($_FILES['upload']['name'])) { $ch = curl_init(); $localfile = $_FILES['upload']['tmp_name']; $fp = fopen($localfile, 'r'); curl_setopt($c

ThinkPHP CodeIgniter URL访问举例

ThinkPHP URL访问: http://localhost/think/index.php/Home/login/func/[name/syt/password/123/] http://localhost/think/index.php/Home/login/func?id=10007&name=yjz&sex=M Codeigniter URL访问: http://localhost/citest/index.php/login?name=syt&id=10001&

curl 访问http请求

post方式请求spring mvc Controller curl -i -H 'content-type: application/json' -X POST -d '{"requestData":"123"}' http://192.168.2.34:8080/test controller:     @RequestMapping(value = Urls.ENTER, method = RequestMethod.POST) @ResponseBody p

curl 访问api-server报403错误

使用curl请求api-server接口时出现下图的错误 临时方案:kubectl create clusterrolebinding system:anonymous --clusterrole=cluster-admin --user=system:anonymous 最终方案:(只给view权限就可以了)kubectl create clusterrolebinding system:anonymous --clusterrole=view --user=system:anonymous