Problems with PHP Curl post data

Source: Internet
Author: User
There was a particularly strange problem today when doing a function of API increments. When I used curl to try Tomcat post data, Tomcat actually made an error, and the data I post
Not correct or to get. However, I use Curl post to a page I wrote myself, and I can get the data in the $_post array.
Why is there such a problem?
The reason is that there is a problem building the number of post data ...
  
     
1 function api_notice_increment ($url, $data)
2 {
3 $ch = Curl_init ();
4 curl_setopt ($ch, curlopt_header,0);
5 curl_setopt ($ch, Curlopt_returntransfer, 1);
6
7 curl_setopt ($ch, Curlopt_url, $url);
8 curl_setopt ($ch, Curlopt_post, 1);
9 curl_setopt ($ch, Curlopt_postfields, $data);
$lst [' rst '] = curl_exec ($ch);
One $lst [' info '] = curl_getinfo ($ch);
curl_close ($ch);
return $lst;
14}
$url = "http://localhost/test/post.api.php tutorial? App=test&act=testact";
$data = Array (
' goods_id ' => ' 1010000001224 ',
' store_id ' => ' 20708 ',
' status ' => ' goodsdownshelf ',
20);
Post.api.php's Code
  
     
<?php
Error_log (Var_export ($_post,1), 3, ' d:/post.txt ');
Execute the above code and generate the Post.txt file in my d:/, which reads as follows:
Array (
' goods_id ' => ' 1010000001224 ',
' store_id ' => ' 20708 ',
' Status ' => ' goodsdownshelf ',
)
Indicates that the data for the post can be obtained normally.
Code on the modification
 
     
1 <?php
2  function api_notice_increment ($url, $data)
3 {
4 $ ch = curl_init ();
5 curl_setopt ($ch, curlopt_header,0);
6 curl_setopt ($ch, Curlopt_returntransfer, 1);
7
8 curl_setopt ($ch, Curlopt_url, $url);
9 curl_setopt ($ch, Curlopt_post, 1);
$data = Http_build_query ($data);
One curl_setopt ($ch, Curlopt_postfields, $data);
$lst [' rst '] = curl_exec ($ch);
$lst [' info '] = curl_getinfo ($ch);
Curl_close ($ch);
return $lst;
}
$url = "Http://localhost/test/post.api.php?app=test&act=testact";
$data = Array (
goods_id ' => ' 1010000001224 ',
' store_id ' => ' 20708 ',
21 ' Status ' => ' goodsdownshelf ',
22);


Api_notice_increment ($url, $data);
Just execute curl_setopt ($ch, Curlopt_postfields, $data); before executing $data = Http_build_query ($data);
Delete d:/post.txt file
Run again.
Open the D:/post.txt file again, which reads as follows:
Array (
' goods_id ' => ' 1010000001224 ',
' store_id ' => ' 20708 ',
' Status ' => ' goodsdownshelf ',
)
If you do not http_build_query the $data, the Java code will not be able to get the post data, http_build_query can be normal after the acquisition.

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.