Login
网站首页 > 文章中心 > 其它

php使用curl发送请求_curl命令发送post请求

作者:小编 更新时间:2023-09-27 17:13:31 浏览量:386人看过

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);

? ? ? 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-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);

http请求有get,post.

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

① file_get_contents();详情见:

下面说使用curl发送.

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

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

在php.ini中将extension=php_curl.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_RETURNTRANSFER,?1);?//?不直接输出到页面

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

curl_close($ch);?//?关闭

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

以上就是土嘎嘎小编为大家整理的php使用curl发送请求,php相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章