PHP利用curl发送HTTP请求的实例代码

吾爱主题 阅读:168 2021-10-19 13:22:00 评论:0

cURL 函数概述

PHP支持的由Daniel Stenberg创建的libcurl库允许你与各种的服务器使用各种类型的协议进行连接和通讯。

libcurl目前支持http、https、ftp、gopher、telnet、dict、file和ldap协议。libcurl同时也支持HTTPS认证、HTTP POST、HTTP PUT、 FTP 上传(这个也能通过PHP的FTP扩展完成)、HTTP 基于表单的上传、代理、cookies和用户名+密码的认证。

PHP中使用cURL实现Get和Post请求的方法

这些函数在PHP 4.0.2中被引入。

实例

因为需要在 php 开发中对接其它接口需要用 php curl 去对接其它接口 我把他们封装成函数 希望能对大家有所帮助。

这里面是封装好的会自动把 data 进行转成 json 格式,同时解码成 php 数组输出。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 <?php function geturl( $url ){      $headerArray = array ( "Content-type:application/json;" , "Accept:application/json" );      $ch = curl_init();      curl_setopt( $ch , CURLOPT_URL, $url );      curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER, FALSE);      curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST, FALSE);      curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1);      curl_setopt( $ch ,CURLOPT_HTTPHEADER, $headerArray );      $output = curl_exec( $ch );      curl_close( $ch );      $output = json_decode( $output ,true);      return $output ; }     function posturl( $url , $data ){      $data = json_encode( $data );       $headerArray = array ( "Content-type:application/json;charset='utf-8'" , "Accept:application/json" );      $curl = curl_init();      curl_setopt( $curl , CURLOPT_URL, $url );      curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER, FALSE);      curl_setopt( $curl , CURLOPT_SSL_VERIFYHOST,FALSE);      curl_setopt( $curl , CURLOPT_POST, 1);      curl_setopt( $curl , CURLOPT_POSTFIELDS, $data );      curl_setopt( $curl ,CURLOPT_HTTPHEADER, $headerArray );      curl_setopt( $curl , CURLOPT_RETURNTRANSFER, 1);      $output = curl_exec( $curl );      curl_close( $curl );      return json_decode( $output ,true); }     function puturl( $url , $data ){    $data = json_encode( $data );    $ch = curl_init(); //初始化CURL句柄    curl_setopt( $ch , CURLOPT_URL, $url ); //设置请求的URL    curl_setopt ( $ch , CURLOPT_HTTPHEADER, array ( 'Content-type:application/json' ));    curl_setopt( $ch , CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出    curl_setopt( $ch , CURLOPT_CUSTOMREQUEST, "PUT" ); //设置请求方式    curl_setopt( $ch , CURLOPT_POSTFIELDS, $data ); //设置提交的字符串    $output = curl_exec( $ch );    curl_close( $ch );    return json_decode( $output ,true); }   function delurl( $url , $data ){    $data = json_encode( $data );    $ch = curl_init();    curl_setopt ( $ch ,CURLOPT_URL, $put_url );    curl_setopt ( $ch , CURLOPT_HTTPHEADER, array ( 'Content-type:application/json' ));    curl_setopt ( $ch , CURLOPT_RETURNTRANSFER, 1);    curl_setopt ( $ch , CURLOPT_CUSTOMREQUEST, "DELETE" );     curl_setopt( $ch , CURLOPT_POSTFIELDS, $data );    $output = curl_exec( $ch );    curl_close( $ch );    $output = json_decode( $output ,true); }   function patchurl( $url , $data ){    $data = json_encode( $data );    $ch = curl_init();    curl_setopt ( $ch ,CURLOPT_URL, $url );    curl_setopt ( $ch , CURLOPT_HTTPHEADER, array ( 'Content-type:application/json' ));    curl_setopt ( $ch , CURLOPT_RETURNTRANSFER, 1);    curl_setopt ( $ch , CURLOPT_CUSTOMREQUEST, "PATCH" );    curl_setopt( $ch , CURLOPT_POSTFIELDS, $data );   //20170611修改接口,用/id的方式传递,直接写在url中了    $output = curl_exec( $ch );    curl_close( $ch );    $output = json_decode( $output );    return $output ; } ?>

以上就是PHP利用curl发送HTTP请求的实例代码的详细内容,更多关于PHP 发送HTTP请求的资料请关注服务器之家其它相关文章!

原文链接:https://www.runoob.com/php/php-ref-curl.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容