简体   繁体   English

如何在curl PHP中正确设置内容长度和base64 url​​编码

[英]How to properly set content length and base64 url encoding in curl php

Am trying to make curl request to an api using parameters below 我正在尝试使用以下参数向API发出curl请求

POST /your api endpoint

Content-type: x-www-form-urlencoded
Content-Length: <length of the request body>
[Authorization: Basic <encoded client_id:client_secret string>]

[& client_id=<your client id>]
[& client_secret=<your client secret>]

This is what the documentation said: 这就是文档所说的:

The client id and secret can also be sent in the Authorization header by encoding the client_id>:<client_secret> string using base64. 客户端ID和密码也可以通过使用base64编码client_id>:<client_secret>字符串在授权标头中发送。

My question is how do I get the requested content-length and also base64 encode the clientid and client secret in the curl header 我的问题是如何获取请求的内容长度,以及如何在curl标头中对clientid和client secret进行base64编码

Here is my effort so far. 到目前为止,这是我的努力。

$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);

To use the Authorisation header and obtain the length of the POST request before sending see below. 要在发送之前使用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 );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM