首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用php发送json post

使用php发送json post
EN

Stack Overflow用户
提问于 2011-06-02 18:47:06
回答 2查看 293.1K关注 0票数 94

我有这个json数据:

代码语言:javascript
复制
{ 
    userID: 'a7664093-502e-4d2b-bf30-25a2b26d6021',
    itemKind: 0,
    value: 1,
    description: 'Saude',
    itemID: '03e76d0a-8bab-11e0-8250-000c29b481aa'
}

并且我需要发布到json url:http://domain/OnLeagueRest/resources/onleague/Account/CreditAccount

使用php如何发送此post请求?

EN

回答 2

Stack Overflow用户

发布于 2011-06-02 19:03:19

您可以使用CURL来实现此目的,请参阅示例代码:

代码语言:javascript
复制
$url = "your url";    
$content = json_encode("your data to be sent");

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
        array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 201 ) {
    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}


curl_close($curl);

$response = json_decode($json_response, true);
票数 143
EN

Stack Overflow用户

发布于 2011-06-02 18:52:33

使用CURL luke :)说真的,这是最好的方法之一,你会得到响应。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6213509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档