php curl发送post请求,向接口发送请求并返回数据。

发布时间:2019-08-07 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php curl发送post请求,向接口发送请求并返回数据。脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

在微信开发中,很多请求都是用curl实现的,并不是跳转的curl或者ajax。

index.php

<?php
//api接口
$url = "server.php";
//发送请求
function httpGet($url){
    $curl = @R_777_436@();
    curl_setopt($curl, CURLOPT_RETURNtransfer, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}
//发送
$str = httpGet($url);
//返回接口的数据
echo $str;
?>

当然,想要触发这个curl请求,还是可以用ajax触发的。

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 &amp;& xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","index.php",true);
xmlhttp.send();
}
</script>
</head>
<body>

<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">请求数据</button>
<div id="myDiv"></div>

</body>
</html>

脚本宝典总结

以上是脚本宝典为你收集整理的php curl发送post请求,向接口发送请求并返回数据。全部内容,希望文章能够帮你解决php curl发送post请求,向接口发送请求并返回数据。所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。