English 中文(简体)
2. 构造一个PHP POST要求,同时提供双轨数据
原标题:Construct a PHP POST request with binary data

I m 在a之后,试图构造一个PHP POST申请,其中包括一些双元数据。 此前的SackOverflow问题。 这些数据正在提交本年度的“:。 http://www.rixstreets.net/api/#addphoto

这是我的PHP代码:

$file = $_FILES[ mediaupload ];
$file_field="@$file[tmp_name]";
$fields = array(
     mediaupload =>$file_field,
     username =>urlencode($_POST["username"]),
     password =>urlencode($_POST["password"]),
     latitude =>urlencode($_POST["latitude"]),
     longitude =>urlencode($_POST["longitude"]),
     datetime =>urlencode($_POST["datetime"]),
     category =>urlencode($_POST["category"]),
     metacategory =>urlencode($_POST["metacategory"]),
     caption =>urlencode($_POST["description"])
);
$fields_string = http_build_query($fields);
echo  FIELDS STRING:   . $fields_string;
$url =  https://www.cyclestreets.net/api/addphoto.json?key=$key ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($ch);

这是我的购买力平价文件的产出:

FIELDS STRING: mediaupload=%40%2Fprivate%2Fvar%2Ftmp%2FphpHjfkRP&username=testing&password=testing&latitude=auto&longitude=auto&datetime=auto&category=cycleparking&metacategory=good&caption=
API RESPONSE: {"request":{"datetime":"1309886656"},"error":{"code":"unknown","message":"The photo was received successfully, but an error occurred while processing it."},"result":{}}

我认为,这意味着除了双向数据的格式外,与请求有关的一切都是科索沃。 谁能告诉我我我我,我做了什么错误?

最佳回答

CURL可以接受低温和低温潜能值油田的低等值。 无需做所有这种透镜和http_build_query() st。 阵列中的@最有可能被划入%40,因此,CURL并不将其视作文件上载尝试。

$fields = array(
     mediaupload =>$file_field,
     username => $_POST["username"),
    etc...

curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
问题回答

rel=“nofollow”> http://build_query 功能产生URL encoded query string, 即“@file.ext”在产出中被编码为“string and cURL don t 知道你试图重上档案。

我的建议将文件上载到<代码>。 电话和人工列入<代码> CURLOPT_POSTFIELDS。

$file = $_FILES[ mediaupload ];
$file_field="@$file[tmp_name]";
$fields = array(
     username =>urlencode($_POST["username"]),
     password =>urlencode($_POST["password"]),
     latitude =>urlencode($_POST["latitude"]),
     longitude =>urlencode($_POST["longitude"]),
     datetime =>urlencode($_POST["datetime"]),
     category =>urlencode($_POST["category"]),
     metacategory =>urlencode($_POST["metacategory"]),
     caption =>urlencode($_POST["description"])
);
$fields_string = http_build_query($fields);
echo  FIELDS STRING:   . $fields_string;
$url =  https://www.cyclestreets.net/api/addphoto.json?key=$key ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,  mediaupload=  . $file_field .  &  . $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($ch);




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签