求指教HTTP客户端正确姿势

我要请求百度的接口,在百度在线调试结果是正确的:

Request:
POST https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_secret=***&client_id=***
x-bce-request-id: dcb8a4311c934a129a57ae751710aaea
User-Agent: Apache-HttpClient/4.5.6 (java 1.5), bce-sdk-java/0.10.132/Linux/3.10.0_3-0-0-22/Java_HotSpot(TM)_64-Bit_Server_VM/25.45-b02/1.8.0_45/en/
Date: Tue, 16 Aug 2022 08:38:45 GMT
Content-Type: application/json; charset=utf-8


Response:
200 OK
Transfer-Encoding: chunked
Cache-Control: no-store
Server: Apache
Connection: keep-alive
Set-Cookie: BAIDUID=61480ED9EB510F1D9D8C3885AEFD017C:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2145916555; path=/; domain=.baidu.com; version=1
Vary: Accept-Encoding
P3p: CP=" OTI DSP COR IVA OUR IND COM "
Date: Tue, 16 Aug 2022 08:38:45 GMT
Content-Type: application/json
{"refresh_token": "25.12f8013ee29bece638403bf636a7da7c.315360000.1975999125.282335-27034120","expires_in": 2592000,"session_key": "9mzdDAE2P6jtm9tbkrmggPZlFRkct3R40920PqK1JIG6F\/B1QLauenuuxO0IYBrCpISh63l1q1Nj+QjVC3VWIfdjIFG5PQ==","access_token": "24.749d6c1074e93282d9e7cf066dd26d55.2592000.1663231125.282335-27034120","scope": "public nlp_simnet nlp_wordemb nlp_comtag nlp_dnnlm_cn brain_nlp_lexer brain_all_scope brain_nlp_comment_tag brain_nlp_dnnlm_cn brain_nlp_word_emb_vec brain_nlp_word_emb_sim brain_nlp_sentiment_classify brain_nlp_simnet brain_nlp_depparser brain_nlp_wordembedding brain_nlp_dnnlm_cn_legacy brain_nlp_simnet_legacy brain_nlp_comment_tag_legacy brain_nlp_lexer_custom brain_nlp_keyword brain_nlp_topic brain_nlp_ecnet brain_nlp_emotion brain_nlp_comment_tag_custom brain_nlp_news_summary brain_nlp_sentiment_classify_custom brain_creation_couplets brain_creation_poem brain_nlp_address brain_nlp_titlepredictor brain_ocr_receipt wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\u6743\u9650 vis-classify_flower lpq_\u5f00\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\u5f00\u653eScope vis-ocr_\u865a\u62df\u4eba\u7269\u52a9\u7406 idl-video_\u865a\u62df\u4eba\u7269\u52a9\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx","session_secret": "a30aa7f9ab11734a71e3ec75ffd1e3c1"}

但是我用laravel的HTTP工具端请求

$response_category = Http::asForm()->get('https://aip.baidubce.com/oauth/2.0/token', [
            'grant_type' => 'client_credentials',
            'client_id' => '***',
            'client_secret' => '***'
        ]);

dd($response_category);

得到的就是

Illuminate\Http\Client\Response {#1472
  #response: GuzzleHttp\Psr7\Response {#1510
    -reasonPhrase: "OK"
    -statusCode: 200
    -headers: array:9 [
      "Cache-Control" => array:1 [
        0 => "no-store"
      ]
      "Connection" => array:1 [
        0 => "keep-alive"
      ]
      "Content-Type" => array:1 [
        0 => "application/json"
      ]
      "Date" => array:1 [
        0 => "Tue, 16 Aug 2022 08:37:44 GMT"
      ]
      "P3p" => array:1 [
        0 => "CP=" OTI DSP COR IVA OUR IND COM ""
      ]
      "Server" => array:1 [
        0 => "Apache"
      ]
      "Set-Cookie" => array:1 [
        0 => "BAIDUID=97F4C5BA75FA274A329ED54C40D72F84:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2145916555; path=/; domain=.baidu.com; version=1"
      ]
      "Vary" => array:1 [
        0 => "Accept-Encoding"
      ]
      "Transfer-Encoding" => array:1 [
        0 => "chunked"
      ]
    ]
    -headerNames: array:9 [
      "cache-control" => "Cache-Control"
      "connection" => "Connection"
      "content-type" => "Content-Type"
      "date" => "Date"
      "p3p" => "P3p"
      "server" => "Server"
      "set-cookie" => "Set-Cookie"
      "vary" => "Vary"
      "transfer-encoding" => "Transfer-Encoding"
    ]
    -protocol: "1.1"
    -stream: GuzzleHttp\Psr7\Stream {#1506
      -stream: stream resource @18
        wrapper_type: "PHP"
        stream_type: "TEMP"
        mode: "w+b"
        unread_bytes: 0
        seekable: true
        uri: "php://temp"
        options: []
      }
      -size: null
      -seekable: true
      -readable: true
      -writable: true
      -uri: "php://temp"
      -customMetadata: []
    }
  }
  #decoded: null
  +"cookies": GuzzleHttp\Cookie\CookieJar {#1491
    -cookies: []
    -strictMode: false
  }
  +"transferStats": GuzzleHttp\TransferStats {#1511
    -request: GuzzleHttp\Psr7\Request {#1497
      -method: "GET"
      -requestTarget: null
      -uri: GuzzleHttp\Psr7\Uri {#1494
        -scheme: "https"
        -userInfo: ""
        -host: "aip.baidubce.com"
        -port: null
        -path: "/oauth/2.0/token"
        -query: "grant_type=client_credentials&client_id=***&client_secret=***"
        -fragment: ""
        -composedComponents: "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=****&client_secret=***"
      }
      -headers: array:3 [
        "User-Agent" => array:1 [
          0 => "GuzzleHttp/7"
        ]
        "Host" => array:1 [
          0 => "aip.baidubce.com"
        ]
        "Content-Type" => array:1 [
          0 => "application/x-www-form-urlencoded"
        ]
      ]
      -headerNames: array:3 [
        "user-agent" => "User-Agent"
        "host" => "Host"
        "content-type" => "Content-Type"
      ]
      -protocol: "1.1"
      -stream: GuzzleHttp\Psr7\Stream {#1496
        -stream: stream resource @16
          wrapper_type: "PHP"
          stream_type: "TEMP"
          mode: "w+b"
          unread_bytes: 0
          seekable: true
          uri: "php://temp"
          options: []
        }
        -size: 0
        -seekable: true
        -readable: true
        -writable: true
        -uri: "php://temp"
        -customMetadata: []
      }
    }
    -response: GuzzleHttp\Psr7\Response {#1510}
    -transferTime: 0.171436
    -handlerStats: array:38 [
      "url" => "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=***&client_secret=***"
      "content_type" => "application/json"
      "http_code" => 200
      "header_size" => 399
      "request_size" => 244
      "filetime" => -1
      "ssl_verify_result" => 0
      "redirect_count" => 0
      "total_time" => 0.171436
      "namelookup_time" => 0.00646
      "connect_time" => 0.046039
      "pretransfer_time" => 0.130398
      "size_upload" => 0.0
      "size_download" => 1648.0
      "speed_download" => 9637.0
      "speed_upload" => 0.0
      "download_content_length" => -1.0
      "upload_content_length" => -1.0
      "starttransfer_time" => 0.171196
      "redirect_time" => 0.0
      "redirect_url" => ""
      "primary_ip" => "183.232.232.56"
      "certinfo" => []
      "primary_port" => 443
      "local_ip" => "172.17.0.2"
      "local_port" => 49130
      "http_version" => 2
      "protocol" => 2
      "ssl_verifyresult" => 0
      "scheme" => "HTTPS"
      "appconnect_time_us" => 130345
      "connect_time_us" => 46039
      "namelookup_time_us" => 6460
      "pretransfer_time_us" => 130398
      "redirect_time_us" => 0
      "starttransfer_time_us" => 171196
      "total_time_us" => 171436
      "appconnect_time" => 0.130345
    ]
    -handlerErrorData: 0
  }
}

所以,我应该怎么办?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

Http::asForm()->post($sUrl,$aParam)->json();

1年前 评论
讨论数量: 7

有没有一种可能,你用的是get,他用的是post

1年前 评论
wongvio (楼主) 1年前
wongvio (楼主) 1年前
yjy1200 (作者) 1年前

Http::asForm()->post($sUrl,$aParam)->json();

1年前 评论

application/json 应该用json而不是from表单

1年前 评论
随波逐流

需要使用 asJson() 指定请求头 headers 才可以


use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\ConnectionException;

try{
    $response = Http::asJson()->timeout($timeout)->post($url, $params);
    if($response->ok()){
        dd($response->json());
    }
}catch(ConnectionException $exception){
    dd($exception);
}
1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!