温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php curl 模拟请求

发布时间:2020-08-10 15:48:17 来源:网络 阅读:244 作者:tinytail 栏目:web开发

很多时候,项目需要以web方式调用外部程序的接口,curl模拟提交便是极好的选择。

public static function curl_post($url, $data = array(), $header = array(), $timeout = 5, $port = 8080) {

       $ch = curl_init();

       curl_setopt($ch, CURLOPT_URL, $url);

       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

       curl_setopt($ch, CURLOPT_TIMEOUT, 10);   //设置cURL允许执行的最长秒数。

       //curl_setopt($ch, CURLOPT_PORT, $port);

       !empty($header) && curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

       curl_setopt($ch, CURLOPT_POST, 1);

       curl_setopt($ch, CURLOPT_POSTFIELDS, $data);


       $result = array();

       $result['result'] = curl_exec($ch);

       if (0 != curl_errno($ch)) {

           $result['error']  = "Error:\n" . curl_error($ch);

       }

       curl_close($ch);


       return $result;

   }

备注:

CURLOPT_TIMEOUT 用于防止请求页面对于无响应返回,自动停止请求外部url

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI