php 使用php发送json帖子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6213509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:40:21  来源:igfitidea点击:

Send json post using php

phpjsonpostcurlhttp-post

提问by Carlos Martins

I have this json data:

我有这个json数据:

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

and I need to post into json url: http://domain/OnLeagueRest/resources/onleague/Account/CreditAccount

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

using php how can I send this post request?

使用 php 如何发送此帖子请求?

回答by Muhammad Zeeshan

You can use CURL for this purpose see the example code:

您可以为此使用 CURL,请参阅示例代码:

$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);

回答by David Riccitelli

Withoutusing any external dependency or library:

使用任何外部依赖项或库:

$options = array(
  'http' => array(
    'method'  => 'POST',
    'content' => json_encode( $data ),
    'header'=>  "Content-Type: application/json\r\n" .
                "Accept: application/json\r\n"
    )
);

$context  = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );

$responseis an object. Properties can be accessed as usual, e.g. $response->...

$response是一个对象。可以像往常一样访问属性,例如$response->...

where $datais the array contaning your data:

其中$data是包含您的数据的数组:

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

Warning: this won't work if the allow_url_fopensetting is set to Offin the php.ini.

警告:如果php.ini 中的allow_url_fopen设置设置为Off,这将不起作用。

If you're developing for WordPress, consider using the provided APIs: https://developer.wordpress.org/plugins/http-api/

如果您正在为WordPress开发,请考虑使用提供的 API:https: //developer.wordpress.org/plugins/http-api/

回答by Knobik

use CURL luke :) seriously, thats one of the best ways to do it AND you get the response.

认真使用 CURL luke :),这是最好的方法之一,并且您会得到响应。

回答by OndrejC

Beware that file_get_contentssolution doesn't close the connection as it should when a server returns Connection: closein the HTTP header.

请注意,当服务器在 HTTP 标头中返回Connection: close时,file_get_contents解决方案不会关闭连接。

CURL solution, on the other hand, terminates the connection so the PHP script is not blocked by waiting for a response.

另一方面,CURL 解决方案会终止连接,因此 PHP 脚本不会因等待响应而被阻止。