婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > PHP CURL與java http使用方法詳解

PHP CURL與java http使用方法詳解

熱門標(biāo)簽:南寧高頻外呼回?fù)芟到y(tǒng)哪家好 深圳外呼系統(tǒng)收費(fèi) 離石地圖標(biāo)注 長沙crm外呼系統(tǒng)業(yè)務(wù) 電話機(jī)器人危險(xiǎn)嗎 400電話申請方法收費(fèi) 江蘇外呼電銷機(jī)器人報(bào)價(jià) 專業(yè)電話機(jī)器人批發(fā)商 400電話辦理福州市

php curl

有時(shí)候我們的項(xiàng)目需要與第三方平臺進(jìn)行交互。舉個(gè)例子。

現(xiàn)在有A、B兩個(gè)平臺。 甲方在最初一段時(shí)間由A實(shí)現(xiàn)了一部分關(guān)鍵業(yè)務(wù)(如用戶信息等)。 然后基于一部分原因,現(xiàn)在有一些業(yè)務(wù)需要B來實(shí)現(xiàn),且實(shí)現(xiàn)程序調(diào)用了一些敏感的接口只能在B方服務(wù)器上跑,那么只能做兩個(gè)平臺之間的交互了。curl 就是這種問題的解決方案。

curl 是一個(gè)php擴(kuò)展,你可以看作一個(gè)可以訪問其他網(wǎng)站的精簡版瀏覽器。
要使用curl 你得在php.ini 中開啟相關(guān)的配置才能使用。
常用的平臺之間交互的數(shù)據(jù)格式 有json、xml等比較流行的數(shù)據(jù)格式。

?php
 @param
 $url  接口地址
 $https 是否是一個(gè)Https 請求
 $post 是否是post 請求
 $post_data post 提交數(shù)據(jù) 數(shù)組格式
function curlHttp($url,$https = false,$post = false,$post_data = array())
{
  $ch = curl_init();                            //初始化一個(gè)curl
  curl_setopt($ch, CURLOPT_URL,$url);     //設(shè)置接口地址 如:http://wwww.xxxx.co/api.php
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//是否把CRUL獲取的內(nèi)容賦值到變量
  curl_setopt($ch,CURLOPT_HEADER,0);//是否需要響應(yīng)頭
  /*是否post提交數(shù)據(jù)*/
  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請求 不驗(yàn)證證書和hosts
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  }
  $output = curl_exec($ch);
  curl_close($ch);
  return $output;
}
?> 

現(xiàn)在 接口地址 http://www.xxxxx.com/api/{sid} 這個(gè)接口地址通過get 方式可以返回一個(gè)user 的 json數(shù)據(jù)格式 ,那么我們怎么去獲取第三方平臺的數(shù)據(jù)

?php
    $sid = 1;
    $url = "http://www.xxxxx.com/api/{$sid}";
    $data = curlHttp($url);
  $user = json_decode($data,true); 
?>

其中$user就是獲取user數(shù)組信息。
在這里 curl 模擬瀏覽器對該域名進(jìn)行了get請求(當(dāng)然,根據(jù)我們在參數(shù)中的設(shè)置,我們也可以去模擬post https 等請求),獲取到了響應(yīng)的數(shù)據(jù)。

java http 實(shí)現(xiàn)了類似php curl 的功能

java 是一門完全面向?qū)ο蟮恼Z言,我覺得除了對象名夠長不容易記憶外。其它的都很好,且它是先編譯成字節(jié)碼然后由java虛擬機(jī)去運(yùn)行的,不像 php 每次都需要去編譯一次以后采取運(yùn)行。
java對php curl 的實(shí)現(xiàn)

文件 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發(fā)送GET方法的請求
   * 
   * @param url
   *      發(fā)送請求的URL
   * @param param
   *      請求參數(shù),請求參數(shù)應(yīng)該是 name1=value1name2=value2 的形式。
   * @return String 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果
   */
  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();
      // 設(shè)置通用的請求屬性
      connection.setRequestProperty("accept", "*/*");
      connection.setRequestProperty("connection","close");
      connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

      // 建立實(shí)際的連接
      connection.connect();

      /*
      // 獲取所有響應(yīng)頭字段
      MapString, ListString>> map = connection.getHeaderFields();
      // 遍歷所有的響應(yīng)頭字段
      for (String key : map.keySet())
      {
        System.out.println(key + "--->" + map.get(key));
      }
      */

      // 定義 BufferedReader輸入流來讀取URL的響應(yīng)
      in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

      String line;

      while ((line = in.readLine()) != null)
      {
        result += line;
      }
    } catch (Exception e) {
      System.out.println("發(fā)送GET請求出現(xiàn)異常!" + e);
      e.printStackTrace();
    }
    // 使用finally塊來關(guān)閉輸入流
    finally {
      try {
        if (in != null) {
          in.close();
        }
      } catch (Exception e2) {
        e2.printStackTrace();
      }
    }
    return result.equals("") ? null : result;
  }

  /**
   * 向指定 URL 發(fā)送POST方法的請求
   * 
   * @param url
   *      發(fā)送請求的 URL
   * @param param
   *      請求參數(shù),請求參數(shù)應(yīng)該是 name1=value1name2=value2 的形式。
   * @return String 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果
   */
  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();
      // 設(shè)置通用的請求屬性
      conn.setRequestProperty("accept", "*/*");
      conn.setRequestProperty("connection", "Keep-Alive");
      conn.setRequestProperty("user-agent",
          "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
      // 發(fā)送POST請求必須設(shè)置如下兩行
      conn.setDoOutput(true);
      conn.setDoInput(true);
      // 獲取URLConnection對象對應(yīng)的輸出流
      out = new PrintWriter(conn.getOutputStream());
      // 發(fā)送請求參數(shù)
      out.print(param);
      // flush輸出流的緩沖
      out.flush();
      // 定義BufferedReader輸入流來讀取URL的響應(yīng)
      in = new BufferedReader(
          new InputStreamReader(conn.getInputStream()));
      String line;
      while ((line = in.readLine()) != null) {
        result += line;
      }
    } catch (Exception e) {
      System.out.println("發(fā)送 POST 請求出現(xiàn)異常!"+e);
      e.printStackTrace();
    }
    //使用finally塊來關(guān)閉輸出流、輸入流
    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數(shù)據(jù)結(jié)果

  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • php curl獲取https頁面內(nèi)容,不直接輸出返回結(jié)果的設(shè)置方法
  • 關(guān)于PHP 如何用 curl 讀取 HTTP chunked 數(shù)據(jù)
  • php使用curl獲取https請求的方法
  • php之curl實(shí)現(xiàn)http與https請求的方法
  • php使用curl訪問https示例分享
  • PHP利用curl發(fā)送HTTP請求的實(shí)例代碼

標(biāo)簽:興安盟 太原 南京 株洲 南昌 白酒營銷 濱州 曲靖

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP CURL與java http使用方法詳解》,本文關(guān)鍵詞  PHP,CURL,與,java,http,使用方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP CURL與java http使用方法詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于PHP CURL與java http使用方法詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 湖北省| 合江县| 博罗县| 班戈县| 岫岩| 阜新| 南乐县| 蓬莱市| 马边| 宜宾市| 电白县| 柘荣县| 八宿县| 盐城市| 错那县| 山阴县| 绥化市| 钦州市| 北票市| 漯河市| 孟津县| 长岭县| 青田县| 襄樊市| 昌吉市| 延津县| 安徽省| 达拉特旗| 同德县| 辛集市| 诸暨市| 霞浦县| 正阳县| 滦平县| 七台河市| 平舆县| 绵竹市| 乌什县| 防城港市| 安徽省| 海城市|