This article mainly introduces the usage of PHP CURL and java http in detail. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
php curl
Sometimes our projects need to interact with third-party platforms. for example.
Now there are two platforms A and B. In the initial period, Party A implemented some key businesses (such as user information, etc.). Then for some reasons, there are some businesses that need to be implemented by B, and the implementation program calls some sensitive interfaces that can only run on the B-side server, so the interaction between the two platforms can only be done. curl is the solution to this problem.
curl is a php extension, you can think of it as a streamlined browser that can access other websites.
To use curl, you must enable the relevant configuration in php.ini to use it.
Commonly used data formats for interaction between platforms include json, xml and other popular data formats.
<?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; } ?>
Now interface addresshttp://www.xxxxx.com/api/{sid} This interface address can return a user through get method json data format, then how do we get the data of the third-party platform
<?php $sid = 1; $url = "http://www.xxxxx.com/api/{$sid}"; $data = curlHttp($url); $user = json_decode($data,true); ?>
$user is to get the user array information.
Here, curl simulates the browser making a get request for the domain name (of course, according to our settings in the parameters, we can also simulate post https and other requests) and obtains the response data.
java http implements functions similar to php curl
java is a completely object-oriented language. I think it is not easy to remember except that the object name is long enough. outside. Everything else is fine, and it is first compiled into bytecode and then run by the Java virtual machine, unlike php which needs to be compiled and run every time.
java implementation of php curl
File 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; } }Then similar php usage is as follows
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数据结果 } }Related recommendations:
PHP’s powerful CURL POST class
PHP's curl disguise source information
ThinkPHP5's CURL operation method on the database
The above is the detailed content of How to use PHP CURL and java http. For more information, please follow other related articles on the PHP Chinese website!

curl和Pythonrequests都是发送HTTP请求的强大工具。虽然curl是一种命令行工具,可让您直接从终端发送请求,但Python的请求库提供了一种更具编程性的方式来从Python代码中发送请求。将curl转换为Pythonrequestscurl命令的基本语法如下所示:curl[OPTIONS]URL将curl命令转换为Python请求时,我们需要将选项和URL转换为Python代码。这是一个示例curlPOST命令:curl-XPOSThttps://example.com/api

在Linux下更新curl版本,您可以按照以下步骤进行操作:检查当前curl版本:首先,您需要确定当前系统中安装的curl版本。打开终端,并执行以下命令:curl--version该命令将显示当前curl的版本信息。确认可用的curl版本:在更新curl之前,您需要确定可用的最新版本。您可以访问curl的官方网站(curl.haxx.se)或相关的软件源,查找最新版本的curl。下载curl源代码:使用curl或浏览器,下载您选择的curl版本的源代码文件(通常为.tar.gz或.tar.bz2

PHP8.1发布:引入curl多个请求并发处理近日,PHP官方发布了最新版本的PHP8.1,其中引入了一个重要的特性:curl多个请求并发处理。这个新特性为开发者提供了一个更加高效和灵活的方式来处理多个HTTP请求,极大地提升了性能和用户体验。在以往的版本中,处理多个请求往往需要通过创建多个curl资源,并使用循环来分别发送和接收数据。这种方式虽然能够实现目

1.HttpURLConnection使用JDK原生提供的net,无需其他jar包,代码如下:importcom.alibaba.fastjson.JSON;importjava.io.BufferedReader;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.net.HttpURLConnection;

从头到尾:如何使用php扩展cURL进行HTTP请求引言:在Web开发中,经常需要与第三方API或其他远程服务器进行通信。而使用cURL进行HTTP请求是一种常见而强大的方式。本文将介绍如何使用php扩展cURL来执行HTTP请求,并提供一些实用的代码示例。一、准备工作首先,确保php已安装cURL扩展。可以在命令行执行php-m|grepcurl查

在linux中,curl是一个非常实用的、用来与服务器之间传输数据的工具,是一个利用URL规则在命令行下工作的文件传输工具;它支持文件的上传和下载,是综合传输工具。curl提供了一大堆非常有用的功能,包括代理访问、用户认证、ftp上传下载、HTTP POST、SSL连接、cookie支持、断点续传等等。

PHPCurl中如何处理网页的301重定向?在使用PHPCurl发送网络请求时,时常会遇到网页返回的301状态码,表示页面被永久重定向。为了正确处理这种情况,我们需要在Curl请求中添加一些特定的选项和处理逻辑。下面将详细介绍在PHPCurl中如何处理网页的301重定向,并提供具体的代码示例。301重定向处理原理301重定向是指服务器返回了一个30

php curl设置cookie的方法:1、创建PHP示例文件;2、通过“curl_setopt”函数设置cURL传输选项;3、在CURL中传递cookie即可。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver CS6
Visual web development tools
