curl算子_PHP: curl_setopt - Manual

I spent a couple of days trying to POST a multi-dimensional array of form fields, including a file upload, to a remote server to update a product. Here are the breakthroughs that FINALLY allowed the script to run as desired.

Firstly, the HTML form used input names like these:

in conjunction with two other form inputs not part of the product array

I used several cURL options, but the only two (other than URL) that mattered were:

curl_setopt($handle, CURLOPT_POST, true);

curl_setopt($handle, CURLOPT_POSTFIELDS, $postfields);

Pretty standard so far.

Note: headers didn't need to be set, cURL automatically sets headers (like content-type: multipart/form-data; content-length...) when you pass an array into CURLOPT_POSTFIELDS.

Note: even though this is supposed to be a PUT command through an HTTP POST form, no special PUT options needed to be passed natively through cURL. Options such as

curl_setopt($handle, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT', 'Content-Length: ' . strlen($fields)));

or

curl_setopt($handle, CURLOPT_PUT, true);

or

curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "PUT);

were not needed to make the code work.

The fields I wanted to pass through cURL were arranged into an array something like this:

$postfields = array("method" => $_POST["method"],

"mode" => $_POST["mode"],

"product" => array("name" => $_POST["product"],

"cost" => $_POST["product"]["cost"],

"thumbnail" => "@{$_FILES["thumbnail"]["tmp_name"]};type={$_FILES["thumbnail"]["type"]}")

);

-Notice how the @ precedes the temporary filename, this creates a link so PHP will upload/transfer an actual file instead of just the file name, which would happen if the @ isn't included.

-Notice how I forcefully set the mime-type of the file to upload. I was having issues where images filetypes were defaulting to octet-stream instead of image/png or image/jpeg or whatever the type of the selected image.

I then tried passing $postfields straight into curl_setopt($this->handle, CURLOPT_POSTFIELDS, $postfields); but it didn't work.

I tried using http_build_query($postfields); but that didn't work properly either.

In both cases either the file wouldn't be treated as an actual file and the form data wasn't being sent properly. The problem was HTTP's methods of transmitting arrays. While PHP and other languages can figure out how to handle arrays passed via forms, HTTP isn't quite as sofisticated. I had to rewrite the $postfields array like so:

$postfields = array("method" => $_POST["method"],

"mode" => $_POST["mode"],

"product[name]" => $_POST["product"],

"product[cost]" => $_POST["product"]["cost"],

"product[thumbnail]" => "@{$_FILES["thumbnail"]["tmp_name"]}");

curl_setopt($handle, CURLOPT_POSTFIELDS, $postfields);

This, without the use of http_build_query, solved all of my problems. Now the receiving host outputs both $_POST and $_FILES vars correctly.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值