2013-05-06 137 views
0

我想使用一个API,我没有得到任何地方。该机制的文档说,“传真文件直接写入到二进制格式要求输入流”PHP上传文件到API

我一直在使用的例子从网络就像试图....

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

但是,当我检查文件在服务器上创建的文件的内容是我为$ data变量定义的路径。 (@/DATA/html/sites/support/FAAPI/TEST.TIF)

我很新的php。我看着php:// input和php:// outout,但我不确定这是否可行。

在我用来调用API的URL上传递文件名,用户名和安全令牌我不知道如何在打开URL后抓取请求输入流来上传文件。

任何帮助表示赞赏。

感谢

+0

尝试双引号来代替单引号括起来的'FileData'路径 – 2013-05-06 06:50:23

回答

0

我会尝试使用file_get_contents()函数有一个专门的流。它的工作原理如下:

  1. 创建一个“上下文”,这是一个描述一个HTTP请求应如何形成:

    • 使用POST请求
    • 与内容类型发送的附加报头:图像/ TIFF
    • 包括TIFF文件
  2. 请求使用S的API URL的内容特殊背景。

骨架代码:

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

这工作就像一个魅力!非常感谢你。 – user2353592 2013-05-06 17:50:11

+0

你能接受答案吗? – hegemon 2013-05-06 18:15:20