PHP CURL Post data error failed creating Formpost data_php Tips

Source: Internet
Author: User
Tags curl curl upload file file upload

In doing micro-credit card using Curl Upload logo image, found an error:

failed creating formpost data

The array in the code is as follows:

$data = Array (' buffer ' => ' @test. jpg ');


Combined with the micro-letter to the document, check the syntax, seemingly did not find anything wrong ...

After the Internet search and verification, and finally found that the problem appears in @ here, if the @ preceded by a \ Escape on it, in the PHP official Bugs.php.net also mentioned (this article is the last to provide a URL)

Final code:

$data = Array (' buffer ' => ' \ @test. jpg ');

Solve the problem.

Bugs Address Reference: https://bugs.php.net/bug.php?id=50060

Let's look at one more example:

Before writing a PHP using Curl upload file code, today burst out a bug,curl error message for "Failed creating Formpost data"

Let's talk about the process of curl uploading files.

$file =array ("upimg" => "@/tmp/tmp.jpg");/file path, preceded by @, indicates file upload. 
$url = ' http://localhosts/upload.php '; 
$ch = Curl_init (); 
curl_setopt ($ch, Curlopt_url, $url); 
curl_setopt ($ch, curlopt_post,true); 
curl_setopt ($ch, Curlopt_postfields, $file); 
Curl_exec ($ch); 

Here are two places to note:

1, $file must be an array

2, upload the file path before add @

3, the path must use the decision path

Now analyze the reason why the "failed creating Formpost data" error occurred

First of all soon to locate the cause of the problem is the path problem, analysis of the program code also found no problem, always do not solve

The reason for the last error log is that the data parameter in the Curl post submits a parameter that begins with the @ sign.

For example, to upload/tmp/tmp.jpg, and submit a profile, so the parameter value is path= '/tmp/tmp.jpg ', input profile desc= ' @test haha ';

According to the manual, there are two ways of submitting data curl Post

1.curl ($ch, Curlopt_postfield, Array (' pic ' => ' @/tmp/tmp.jpg ', ' filename ' => ' tmp '); 
 
2.curl ($ch, Curlopt_postfield, ' pic=@/tmp/tmp.jpg&filename=tmp ') 

The first method must be used in case of uploading files

curl($ch, CURLOPT_POSTFIELD, array('pic'=>'@/tmp/tmp.jpg', 'filename'=>'tmp'); 

So if the first character is at the @ character after the content will be resolved to address, if the address does not exist will throw the error, want to introduce DESC @test this path is non-existent

The second method only treats it as a normal string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.