В свободное время я немного поиграл, и мне показалось, что на моей тестовой системе работает следующее.Функция предназначена для очень простой конечной точки php, а не для удаленной конечной точки url / api.
<code>$file=sprintf('%s%slamb_vindaloo.pdf', __DIR__, DIRECTORY_SEPARATOR );
define( 'BEARER_TOKEN', '74e793ea86b6e06d5d971454a955c48012a422d7694d3463c23f69ef758f62db' );
define( 'AUTH_URL', 'https://sentinel/demo/stack/pdf-receiver.php' );
define( 'CA_CERTIFICATE', 'c:/wwwroot/cacert.pem' );
function getToken( $file=false ){
if( !$file )return;
$curl = curl_init();
$params = array(
CURLOPT_URL => AUTH_URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POST => true,
CURLOPT_NOBODY => false,
CURLOPT_HTTPHEADER => array(
sprintf( "Authorization: Bearer %s", BEARER_TOKEN ),
"Cache-Control: no-cache",
"Content-Type: application/octet-stream", # <----- set the content-type
sprintf("Content-Disposition: form-data; name='file'; filename='%s'", $file ),
),
CURLOPT_POSTFIELDS => array (
'filename' => new CURLFile( $file, mime_content_type( $file ), pathinfo( $file, PATHINFO_FILENAME ) )
),
CURLOPT_CAINFO => CA_CERTIFICATE, # <------ if the endpoint is SSL it is advisable to use a valid cacert.pem file!!
CURLOPT_SSL_VERIFYPEER => false, # <------ if possible set this to true
CURLOPT_SSL_VERIFYHOST => 2 # <------ true/false are not options for this
);
curl_setopt_array( $curl, $params );
/*
Return an object with the various return values
rather than doing any printing from within the
function
*/
$res=(object)array(
'response' => curl_exec( $curl ),
'info' => (object)curl_getinfo( $curl ),
'errors' => curl_error( $curl )
);
curl_close( $curl );
return $res;
}
/* call the function */
$results=getToken( $file );
/* process the response accordingly */
if( $results->info->http_code==200 ){
printf('<pre>%s
', $ results-> response);}
Простая конечная точка PHP:
<?php
#
# PDF File Receiver for curlFile Tests....
#
$data=file_get_contents( 'php://input' );
echo base64_encode( $data );
?>
Ответ, чтобы выделить успех, был следующим:
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS02NGQ5ZGYyYTE3ZmE1MGNkDQpDb250ZW50LURpc3Bvc...etc etc