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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘