php curl
有时候我们的项目需要与第三方平台进行交互。举个例子。
现在有a、b两个平台。 甲方在最初一段时间由a实现了一部分关键业务(如用户信息等)。 然后基于一部分原因,现在有一些业务需要b来实现,且实现程序调用了一些敏感的接口只能在b方服务器上跑,那么只能做两个平台之间的交互了。curl 就是这种问题的解决方案。
curl 是一个php扩展,你可以看作一个可以访问其他网站的精简版浏览器。
要使用curl 你得在php.ini 中开启相关的配置才能使用。
常用的平台之间交互的数据格式 有json、xml等比较流行的数据格式。
<?php
@param
$url 接口地址
$https 是否是一个https 请求
$post 是否是post 请求
$post_data post 提交数据 数组格式
function curlhttp($url,$https = false,$post = false,$post_data = array())
{
$ch = curl_init(); //初始化一个curl
curl_setopt($ch, curlopt_url,$url); //设置接口地址 如:http://wwww.xxxx.co/api.php
curl_setopt($ch, curlopt_returntransfer,1);//是否把crul获取的内容赋值到变量
curl_setopt($ch,curlopt_header,0);//是否需要响应头
/*是否post提交数据*/
if($post){
curl_setopt($ch,curlopt_post,1);
if(!empty($post_data)){
curl_setopt($ch,curlopt_postfields,$post_data);
}
}
/*是否需要安全证书*/
if($https)
{
curl_setopt($ch, curlopt_ssl_verifypeer, false); // https请求 不验证证书和hosts
curl_setopt($ch, curlopt_ssl_verifyhost, false);
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>
现在 接口地址 http://www.xxxxx.com/api/{sid} 这个接口地址通过get 方式可以返回一个user 的 json数据格式 ,那么我们怎么去获取第三方平台的数据
<?php
$sid = 1;
$url = "http://www.xxxxx.com/api/{$sid}";
$data = curlhttp($url);
$user = json_decode($data,true);
?>
其中$user就是获取user数组信息。
在这里 curl 模拟浏览器对该域名进行了get请求(当然,根据我们在参数中的设置,我们也可以去模拟post https 等请求),获取到了响应的数据。
java http 实现了类似php curl 的功能
java 是一门完全面向对象的语言,我觉得除了对象名够长不容易记忆外。其它的都很好,且它是先编译成字节码然后由java虚拟机去运行的,不像 php 每次都需要去编译一次以后采取运行。
java对php curl 的实现
文件 tool.httprequest
package tool;
import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.printwriter;
import java.net.url;
import java.net.urlconnection;
import java.util.list;
import java.util.map;
import java.net.urlencoder;
import log.log;
public class httprequest
{
/**
* 向指定url发送get方法的请求
*
* @param url
* 发送请求的url
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return string 所代表远程资源的响应结果
*/
public static string get(string url,string param)
{
string result = "";
bufferedreader in = null;
try {
string urlnamestring = null;
if(param == null)
urlnamestring = url;
else
urlnamestring = url + "?" + param;
//system.out.println("curl http url : " + urlnamestring);
url realurl = new url(urlnamestring);
// 打开和url之间的连接
urlconnection connection = realurl.openconnection();
// 设置通用的请求属性
connection.setrequestproperty("accept", "*/*");
connection.setrequestproperty("connection","close");
connection.setrequestproperty("user-agent","mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)");
// 建立实际的连接
connection.connect();
/*
// 获取所有响应头字段
map<string, list<string>> map = connection.getheaderfields();
// 遍历所有的响应头字段
for (string key : map.keyset())
{
system.out.println(key + "--->" + map.get(key));
}
*/
// 定义 bufferedreader输入流来读取url的响应
in = new bufferedreader(new inputstreamreader(connection.getinputstream()));
string line;
while ((line = in.readline()) != null)
{
result += line;
}
} catch (exception e) {
system.out.println("发送get请求出现异常!" + e);
e.printstacktrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (exception e2) {
e2.printstacktrace();
}
}
return result.equals("") ? null : result;
}
/**
* 向指定 url 发送post方法的请求
*
* @param url
* 发送请求的 url
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return string 所代表远程资源的响应结果
*/
public static string post(string url, string param) {
printwriter out = null;
bufferedreader in = null;
string result = "";
try {
url realurl = new url(url);
// 打开和url之间的连接
urlconnection conn = realurl.openconnection();
// 设置通用的请求属性
conn.setrequestproperty("accept", "*/*");
conn.setrequestproperty("connection", "keep-alive");
conn.setrequestproperty("user-agent",
"mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)");
// 发送post请求必须设置如下两行
conn.setdooutput(true);
conn.setdoinput(true);
// 获取urlconnection对象对应的输出流
out = new printwriter(conn.getoutputstream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义bufferedreader输入流来读取url的响应
in = new bufferedreader(
new inputstreamreader(conn.getinputstream()));
string line;
while ((line = in.readline()) != null) {
result += line;
}
} catch (exception e) {
system.out.println("发送 post 请求出现异常!"+e);
e.printstacktrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(ioexception ex){
ex.printstacktrace();
}
}
return result;
}
}
然后类似php的使用如下
web.app.controller.indexcontroller
package web.app.controller;
import tool.httprequest;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.responsebody;
import net.sf.json.jsonobject;
@controller
@requestmapping("index")
public class indexcontroller
{
@requestmapping(value="index",method={requestmethod.get,requestmethod.post},produces="text/html;charset=utf-8")
@responsebody
public string index()
{
string sid = "1";
string apiurl = "http://www.xxxxx.com/api/" +sid;
string data = httprequest.get(apiurl,null); //开始模拟浏览器请求
jsonobject json = jsonobject.fromobject(data); //解析返回的json数据结果
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。