亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 curl php 中正確設置內容長度和 base64 url?? 編碼

如何在 curl php 中正確設置內容長度和 base64 url?? 編碼

PHP
Helenr 2022-01-02 19:55:16
我正在嘗試使用下面的參數向 api 發出 curl 請求POST /your api endpointContent-type: x-www-form-urlencodedContent-Length: <length of the request body>[Authorization: Basic <encoded client_id:client_secret string>][& client_id=<your client id>][& client_secret=<your client secret>]這是文檔所說的:也可以通過client_id>:<client_secret>使用 base64對字符串進行編碼,在 Authorization 標頭中發送客戶端 ID 和機密。我的問題是如何獲取請求的內容長度以及如何在 curl 標頭中對 clientid 和 client 機密進行 base64 編碼這是我到目前為止的努力。$id = 'xxxx';$secret='xxxx';$url = "//myapi.com/endpoint";$ch = curl_init();curl_setopt($ch,CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([    'client_id'=>$id,    'client_secret'=>$secret]));curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          'Content-Type: application/x-www-form-urlencoded',    'Content-Length: ',                                                                            "Authorization: Basic")                                                                       );  echo $data = curl_exec($ch);curl_close($ch);
查看完整描述

1 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

要在發送前使用 Authorization 標頭并獲取 POST 請求的長度,請參見下文。


$id = 'xxxx';

$secret='xxxx';

$url = "//myapi.com/endpoint";

/*

    If the API endpoint is https it will be necessary to use certificate verification

    techniques which require you have a valid cacert.pem file

    You can download a valid copy from:-


    https://curl.haxx.se/docs/caextract.html

*/

$cacert='/path/to/cacert.pem';


/*

    The POST request body needs to have it's length measured for

    the correct `Content-Length` header

*/

$params=array(

    'client_id'     =>  $id,

    'client_secret' =>  $secret

);

$payload=http_build_query( $params );


/*

    create the base64 encoded clientid/secret portion of the auth header

*/

$encoded = base64_encode( sprintf('%s:%s', $id, $secret ) );


/*

    create the full auth header string

*/

$authheader = sprintf('Authorization: Basic %s', $encoded );


/*

    create the content-length header string - use `strlen` to get the length!

*/

$contentlength = sprintf( 'Content-Length: %s', strlen( $payload ) );


/*

    construct the headers array that will be sent with the request

*/

$headers=array(                                                                        

    'Content-Type: application/x-www-form-urlencoded',  

    $contentlength,                                                                          

    $authheader                                                                       

);



/*

    initialise the request and set options 

*/

$ch = curl_init();

curl_setopt( $ch,CURLOPT_URL, $url );

if( parse_url( $url,PHP_URL_SCHEME )=='https' ){

    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );

    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );

    curl_setopt( $ch, CURLOPT_CAINFO, $cacert );

}

curl_setopt( $ch, CURLOPT_POST, true );

curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );

curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );

curl_setopt( $ch, CURLOPT_USERAGENT, 'Google Chrome' );

curl_setopt( $ch, CURLINFO_HEADER_OUT, false );


$data = curl_exec( $ch );

$errs = curl_error( $ch );

$info = (object)curl_getinfo( $ch );

curl_close( $ch );


if( info->http_code==200 ){

    /* OK */

    print_r( $data );

} else {

    print_r( $info );

}


查看完整回答
反對 回復 2022-01-02
  • 1 回答
  • 0 關注
  • 280 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號