PHP上传文件到API

I am trying to use an API and I am not getting anywhere. The documention says "The document to fax is written directly to the Request Input Stream in binary format"

I have tried using examples from the web like....

$xSendURL = 'http:/example.com/default.ashx?OPT=SendFax&UserName=johndoe...';

# New data
$data = array('FileData' => '@/DATA/html/sites/support/FAAPI/TEST.TIF');

# Create a connection
$ch = curl_init();

# Setting our options
curl_setopt($ch, CURLOPT_URL, $xSendURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);

# Get the response
$response = curl_exec($ch);
curl_close($ch);

But when I check the file that is created on the server the contents of the file is the path that I defined for the $data variable. (@/DATA/html/sites/support/FAAPI/TEST.TIF)

I am very new at php. I looked into php://input and php://outout but I am not sure if that would work.

On the URL that I use to invoke the API I pass the filename, username, and security tokens I am not sure how I would grab the Request Input Stream to upload the file after opening the URL.

Any help is appreciated.

Thanks

I would try using file_get_contents() with a specialized stream. It works as follows:

  1. Create a "context" which is a description how a HTTP request should be formed:

    • use a POST request
    • send an additional header with Content-Type: image/tiff
    • include contents of the tiff file
  2. Request the API URL using the specified context.

Skeleton code:

$data = file_get_contents('/DATA/html/sites/support/FAAPI/TEST.TIF');

$options = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: image/tiff',
        'content' => $data,
    )
);

$context = stream_context_create($options);
$result = file_get_contents($xSendURL, false, $context);