且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

发送json post使用php

更新时间:2023-02-24 15:19:48

更新



这是一个很老的答案。我发现 Guzzle库非常易于在PHP中使用HTTP。






你真的需要发布JSON数据吗?



我知道的***的方法是通过Zend Framework的HTTP客户端。



有关原始帖子详情,请参阅此处 - http://framework.zend.com/manual/en/zend.http.client.advanced.html#zend.http.client.raw_post_data



这将像

  $ data = array(
'userID' =>'a7664093-502e-4d2b-bf30-25a2b26d6021',
'itemKind'=> 0,
'value'=> 1,
'description'=& boasaudaáo。',
'itemID'=>'03e76d0a-8bab-11e0-8250-000c29b481aa'
);

$ json = json_encode($ data);

$ client = new Zend_Http_Client($ uri);
$ client-> setRawData($ json,'application / json') - > request('POST');


I have this data:

{ 
    userID: 'a7664093-502e-4d2b-bf30-25a2b26d6021',
    itemKind: 0,
    value: 1,
    description: 'Boa saudaÁ„o.',
    itemID: '03e76d0a-8bab-11e0-8250-000c29b481aa'
}

and I need to post into json url: http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/Account/CreditAccount

using php how can I send this post?

Update

This is a pretty old answer. I find the Guzzle library very easy to use for working with HTTP in PHP.


Do you actually need to post JSON data? If so, you're looking at a raw HTTP post.

The best method I know of to do this is via Zend Framework's HTTP client.

See here for raw post details - http://framework.zend.com/manual/en/zend.http.client.advanced.html#zend.http.client.raw_post_data

It would be something like

$data = array(
    'userID'      => 'a7664093-502e-4d2b-bf30-25a2b26d6021',
    'itemKind'    => 0,
    'value'       => 1,
    'description' => 'Boa saudaÁ„o.',
    'itemID'      => '03e76d0a-8bab-11e0-8250-000c29b481aa'
);

$json = json_encode($data);

$client = new Zend_Http_Client($uri);
$client->setRawData($json, 'application/json')->request('POST');