如何使用php发送分段请求 [英] How to send a multipart request with php

查看:120
本文介绍了如何使用php发送分段请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从php中发送经过多部分/表单数据编码的帖子请求.我已经在这里找到此代码:

I'd like to know how I can send a multipart/form-data encoded post request out of php. I already found this code here:

$destination = "http://yourdomain.com/yoururl";



$eol = "\r\n";

$data = '';


$mime_boundary=md5(time());


$data .= '--' . $mime_boundary . $eol;

$data .= 'Content-Disposition: form-data; name="somedata"' . $eol . $eol;

$data .= "Some Data" . $eol;

$data .= '--' . $mime_boundary . $eol;

$data .= 'Content-Disposition: form-data; name="somefile"; 
filename="filename.ext"' . $eol;

$data .= 'Content-Type: text/plain' . $eol;

$data .= 'Content-Transfer-Encoding: base64' . $eol . $eol;

$data .= chunk_split(base64_encode("Some file content")) . $eol;

$data .= "--" . $mime_boundary . "--" . $eol . $eol; // finish with two 
eol's!!


$params = array('http' => array(

                  'method' => 'POST',

                  'header' => 'Content-Type: multipart/form-data; boundary=' 
. $mime_boundary . $eol,

                  'content' => $data


               ));


$ctx = stream_context_create($params);

$response = @file_get_contents($destination, FILE_TEXT, $ctx);

( http://vedovini.net/2009/08/posting-multipart-form-data-using-php/) 我的问题是:如何定义应发送的文件? (只是为了说明我喜欢做的事情:用户使用简单的http格式上传文件,php脚本接收到该文件,然后将其重新发送"到另一个url).我还必须在json数组中与文件一起发送1个变量.

(http://vedovini.net/2009/08/posting-multipart-form-data-using-php/) My question is: How can I define the file that should be sent? (Just to explain what I like to do: The User uploads a file with a simple http form, the php script receives it and should "resend" the file to another url). I also have to send 1 variable in a json array along with the file.

推荐答案

使用cURL代替file_get_contents,关于将其用于多部分POST提交也存在类似的问题

Use cURL instead of file_get_contents, there is a similar question about using it with multipart POST submissions here.

这篇关于如何使用php发送分段请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆