为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 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 关注
  • 326 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号