Ec (2); & nbsp; today, a particularly strange problem occurs when creating an api increment function. When I use curl to restore the atpost data, tomcat returns an error because the post data is incorrect or incorrect. However, if I use curlpost to write a page to myself, we can get data in the $ _ post array. Why is this problem? The reason is that the number of post data is incorrect... 1functionapi _ script ec (2); script
Today, when I made an api incremental feature, I encountered a particularly strange problem. When I tried to use curl to post tomcat data, tomcat reported an error.
Incorrect or incorrect. However, if I use curl post to write a page to myself, we can get data in the $ _ post array.
Why is this problem?
The reason is that the number of post data is incorrect...
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 );
10 $ lst ['rst'] = curl_exec ($ ch );
11 $ lst ['info'] = curl_getinfo ($ ch );
12 curl_close ($ ch );
13 return $ lst;
14}
15 $ url = "http: // localhost/test/post. api. php tutorial? App = test & act = testact ";
16 $ data = array (
17 'goods _ id' => '123 ',
18 'store _ id' => '123 ',
19 'status' => 'goodsdownshelf ',
20 );
// Post. api. php code
error_log(var_export($_post,1),3,'d:/post.txt');
Execute the above Code. In my d:/generated post.txt file, the content is as follows:
Array (
'Goods _ id' => '20180101 ',
'Store _ id' => '123 ',
'Status' => 'goodsdownshelf ',
)
It indicates that post data can be obtained normally.
Modified code
1
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);
10 $data = http_build_query($data);
11 curl_setopt($ch, curlopt_postfields, $data);
12 $lst['rst'] = curl_exec($ch);
13 $lst['info'] = curl_getinfo($ch);
14 curl_close($ch);
15 return $lst;
16 }
17 $url = "http://localhost/test/post.api.php?app=test&act=testact";
18 $data = array (
19 'goods_id' => '1010000001224',
20 'store_id' => '20708',
21 'status' => 'goodsdownshelf',
22 );
23
24
25 api_notice_increment($url,$data);
Just execute the $ data = http_build_query ($ data); operation before performing curl_setopt ($ ch, curlopt_postfields, $ data.
Delete the d:/post.txt File
Run again.
Open the d:/post.txt file again with the following content:
Array (
'Goods _ id' => '20180101 ',
'Store _ id' => '123 ',
'Status' => 'goodsdownshelf ',
)
If you do not perform http_build_query for $ data, the java code will not be able to obtain the post data. After http_build_query, the data can be normally obtained.