第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何在 curl php 中正確設(shè)置內(nèi)容長度和 base64 url?? 編碼

如何在 curl php 中正確設(shè)置內(nèi)容長度和 base64 url?? 編碼

PHP
Helenr 2022-01-02 19:55:16
我正在嘗試使用下面的參數(shù)向 api 發(fā)出 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對字符串進(jìn)行編碼,在 Authorization 標(biāo)頭中發(fā)送客戶端 ID 和機(jī)密。我的問題是如何獲取請求的內(nèi)容長度以及如何在 curl 標(biāo)頭中對 clientid 和 client 機(jī)密進(jìn)行 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貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個贊

要在發(fā)送前使用 Authorization 標(biāo)頭并獲取 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 );

}


查看完整回答
反對 回復(fù) 2022-01-02
  • 1 回答
  • 0 關(guān)注
  • 281 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號