douzhi3667 2013-05-06 06:46
浏览 61
已采纳

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

  • 写回答

1条回答 默认 最新

  • dsm0688 2013-05-06 08:50
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题