记一次php curl给java提交数据失败的经历

最近由于业务需求需要对接一个对外接口(对方为java),简单来说就是需要通过post方式来给对方提交数据。但是对方一直收不到数据。简化代码如下:

$url = 'http://www.test.com';
$data = array(
  'name' => 'test',
  'age' => 123,
); 
$curl = curl_init (); // 启动一个CURL会话
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
curl_setopt ( $curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
$result = curl_exec ( $curl ); // 执行操作
curl_close ( $curl ); 

后来发现是由于提交数据时header头的content-type跟对方不一致所导致。

  • CURLOPT_POSTFIELDS 这个参数字段数据为一个数组时,Content-Type头将会被设置成multipart/form-data(数据格式);
  • 而这个参数字符串类似’para1=val1&para2=val2&…’时,Content-Type头将会被设置成application/x-www-form-urlencoded(表单格式),就像表单提交的一样。

对方接收的Content-Type为application/x-www-form-urlencoded(表单格式),而现在PHP curl post提交数据时Content-Type为multipart/form-data(数据格式)。这才导致对方无法通过POST来接收数据。现在只需要把代码中

curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data);
//更改为
curl_setopt ( $curl, CURLOPT_POSTFIELDS, http_build_query($data));

就OK了。

在PHP中通过POST方式接收的普通数据直接用$_POST就可以获取到,而如果是二进制流数据需要用$_FILES来获取。如果接收的数据是二进制流数据(文件)还用$_POST来接收,那只获取到的是这个文件的路径名字符串。

另外通过PHP的curl要发送文件,在文件名前面加上@前缀并使用完整路径就可以了。例如:

//$data['file']就是要发送的文件

$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);

3,223 Responses

  1. Pyridium Cheap Cod Accepted. Order Online Pyridium Discount Without Dr Approval. Want To Buy Generic Pyridium Website Cash Delivery Overseas Cialis Im Flugzeug Mitnehmen Viagra 100mg Preis Apotheke cialis online Amoxicillin Absorbtion Amount Propecia 8 Months Medicine Alternativa Al Viagra

  2. I would like to voice my respect for your generosity supporting men and women that absolutely need assistance with this area of interest. Your personal commitment to passing the message along ended up being remarkably useful and have all the time allowed employees like me to arrive at their aims. Your amazing important report means so much to me and even more to my office colleagues. Best wishes; from everyone of us.

  3. I simply wanted to appreciate you yet again. I do not know the things I would have handled without the actual recommendations shared by you directly on such concern. It actually was a real frightening condition in my position, but being able to view a well-written avenue you handled the issue made me to weep for joy. I am grateful for your support and as well , wish you are aware of a powerful job you’re doing educating other individuals via your web site. I am sure you have never got to know any of us.

  4. I’ll right away take hold of your rss feed as I caan not in finding your e-mail subscription link or newsletter service.
    Do you’ve any? Pleease permit me understand so that I could subscribe.

    Thanks.

  5. You can certainly see your skills in the article you write.
    The world hopes for more passionate writers like you who are not afraid
    to say how they believe. All the time go after your heart.

  6. Thanks for any other magnificent post. The place else could anyone
    get that type of information in such a perfect means of writing?

    I have a presentation next week, and I’m at the look for such
    info.

  7. I really like your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you?
    Plz answer back as I’m looking to create my own blog and would like to find out
    where u got this from. many thanks

  8. Hey there I am so happy I found your website, I really found you by accident, while I was looking on Askjeeve for something else, Anyhow
    I am here now and would just like to say thank you for a tremendous post and a all round enjoyable blog
    (I also love the theme/design), I don’t have time to read through it all at
    the moment but I have book-marked it and also added
    your RSS feeds, so when I have time I will be back to read
    more, Please do keep up the awesome job.

  9. Thanks , I’ve recently been searching for info about this subject
    for ages and yours is the greatest I’ve came upon so far.
    But, what concerning the conclusion? Are you certain about
    the supply?

  10. May I just say what a relief to find an individual who really understands what they’re discussing online. You actually know how to bring an issue to light and make it important. More and more people have to look at this and understand this side of your story. It’s surprising you’re not more popular since you definitely possess the gift.

    https://www.manumanu.com – manu manumanu

  11. I believe everything said was very reasonable. But, what
    about this? suppose you were to create a killer headline?
    I am not saying your information isn’t solid.,
    but suppose you added a headline to possibly get folk’s attention? I mean 记一次php
    curl给java提交数据失败的经历 | Lyy's blog is a little plain.
    You ought to peek at Yahoo’s home page and watch how they create news titles to grab people to open the links.
    You might add a related video or a picture or two
    to grab readers excited about everything’ve written. In my opinion, it would bring
    your blog a little livelier.

  12. Do you have a spam problem on this website; I also am a blogger,
    and I was curious about your situation; we have created some nice
    practices and we are looking to trade methods with other folks,
    why not shoot me an email if interested.

  13. It is the best time to make a few plans for the future and it is time to
    be happy. I have read this publish and if I may I
    want to recommend you few fascinating things or advice.
    Perhaps you can write subsequent articles regarding this article.
    I want to read more issues approximately it!

  14. I do not even know how I ended up right here, however I assumed this
    submit used to be good. I do not recognize who you’re but
    certainly you are going to a well-known blogger if you aren’t already.
    Cheers!

  15. hello!,I really like your writing very so much! share
    we keep in touch more about your post on AOL?
    I require a specialist in this area to solve my problem.
    May be that’s you! Looking ahead to see you.

  16. It is the best time to make some plans for the future and it is time to be happy.

    I’ve read this post and if I could I desire to suggest you few interesting things or
    suggestions. Maybe you can write next articles referring
    to this article. I want to read even more things about it!

  17. I’ll right away seize your rss as I can’t find your
    email subscription hyperlink or e-newsletter service.
    Do you have any? Please let me know so that I may just subscribe.
    Thanks.

  18. We’re a gaggle of volunteers and opening a new scheme in our community.
    Your site offered us with valuable information to work on. You have performed a formidable activity and
    our entire community can be thankful to you.

  19. You have made some really good points there. I looked on the internet to
    find out more about the issue and found most people will go along with your views on this web site.

  20. I’m impressed, I must say. Seldom do I encounter a blog that’s equally educative and interesting,
    and without a doubt, you’ve hit the nail on the head.
    The issue is something too few men and women are speaking intelligently
    about. I’m very happy that I stumbled across this during
    my search for something regarding this.

  21. You made some good points there. I checked on the net to find out more about the issue and found
    most people will go along with your views on this site.

  22. My spouse and I stumbled over here different page and thought I may as well check things out.
    I like what I see so i am just following you. Look forward to looking
    over your web page repeatedly.

  23. Hello I am so grateful I found your blog page, I really found you by mistake, while I
    was researching on Askjeeve for something else, Anyways I am here now and would just like to say many thanks for a marvelous post and a all round entertaining blog (I also
    love the theme/design), I don’t have time to look over it all at the moment but I have saved it and also included your RSS feeds, so when I have time
    I will be back to read a great deal more, Please do keep up
    the awesome job.

  24. We stumbled over here by a different page and thought I should check things out.

    I like what I see so now i’m following you.
    Look forward to going over your web page yet again.