【问题标题】:Send json post using php使用 php 发送 json 帖子
【发布时间】:2011-09-06 23:55:04
【问题描述】:

我有这个 json 数据:

{ 
    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请求?

【问题讨论】:

  • 提供更多细节或代码
  • 我只需要发送带有 userID、itemKind、value、description 和 itemID 的 json 帖子
  • @Gumbo Chrome 不同意你的观点;)
  • @Phil:JSON 不是 JavaScript,反之亦然。 Chrome 可能会接受该代码,因为它具有 JavaScript 解释器。但是如果你用JSON.parse去解析那个代码,肯定会失败。
  • @Gumbo 感谢您提供额外信息。问题是未引用的键吗?

标签: php json post curl http-post


【解决方案1】:

您可以为此目的使用 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);

【讨论】:

  • 我有比 JSON 更多的值——是的,我也有 JSON 值。我现在该怎么做?我总共要发布三个值: title=somevalue&hash=somevalue&json = JSON VALUE 。现在如何使用 php 做到这一点?
  • 这在我的情况下不起作用 (stackoverflow.com/questions/34021817/…)。我的 nodejs 收到了 {}。知道为什么吗?
  • 如果你对这个和原生 file_get_contents 方法进行基准测试,它们的速度差不多
  • @LIGHT - $content = http_build_query( array( 'key1' => 'value1', 'key2' => 'value2', 'json' => json_encode($array_to_become_json) ) ); 你之前准备好了$array_to_become_json = array(...)
【解决方案2】:

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

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

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

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

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

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

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

【讨论】:

  • 我知道我迟到了几年,但是用这种方法可以得到响应头吗?
  • 太棒了,感谢您提到这样做没有任何扩展!
  • 总是欢迎没有扩展。我不知道 WP 有一个标准化的 API。非常感谢您提供的信息!
  • @solarshado 原始响应标头可以使用stream_get_meta_data 获取。
  • 一个小的改进建议,您也可以将标头放在一个数组中,特别是如果需要添加更多的标头,例如授权。在这种情况下,应该添加“\r\n”。
【解决方案3】:

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

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

【讨论】:

  • 伟大的观察
【解决方案4】:

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

【讨论】:

  • curl 并不总是启用......有时你需要它来做旧时尚的方式......
猜你喜欢
  • 2022-08-07
  • 2018-10-05
  • 2011-06-23
  • 1970-01-01
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多