【问题标题】:HTTP POST in PHPPHP 中的 HTTP POST
【发布时间】:2019-12-07 11:48:15
【问题描述】:

我正在尝试使用 PHP 发布 JSON 有效负载,但无法正常工作。我有以下适用于 Shell 的 CURL 命令以及以下也适用的 Python 代码,但需要 PHP 实现相同的功能。

外壳上的 CURL:

curl -H "Content-Type: application/json" -X POST -d '{"Content":" <CONTENT OF THE MESSAGE> "}' <URL of the node receiving POST data>

Python:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests

headers = {
  'Content-type': 'application/json',
}

auth = '<URL>'

line1 = '<CONTENT of message>'

data = '{"Content":"%s"}' % (line1)
response = requests.post(url = auth, headers = headers, data = data)

到目前为止我在 PHP 中所拥有的(不工作):

$data = array("Content" => "<CONTENT of the message>");                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('<URL>');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

提前非常感谢任何和所有帮助!

【问题讨论】:

标签: php curl post http-post php-curl


【解决方案1】:

看起来你在那里做的一切都是正确的(除了第 10+11 行可怕的缩进让你看起来好像错过了 ) 而实际上你没有),你只是缺少错误-检查代码,调试,尝试:

$stderrh=tmpfile();
curl_setopt_array($ch,[CURLOPT_VERBOSE=>1,CURLOPT_STDERR=>$stderrh]);
$result = curl_exec($ch);
rewind($stderrh); // https://bugs.php.net/bug.php?id=76268
var_dump(stream_get_contents($stderrh),$result);

详细日志应该告诉你问题是什么,它说了什么?

(你也错过了 &lt;?php 在开头,你可能想在结尾添加 var_dump($result); 。为了加快速度,你可以添加 CURLOPT_ENCODING=&gt;'' 以使 curl 使用压缩进行传输,如果响应是可压缩的(如 JSON 或 HTML 或文本),通常会加快处理速度)

【讨论】:

    【解决方案2】:

    试试这个代码。

        $ch = curl_init( );
        $data = array("Content" => "<CONTENT of the message>");
        $headers = array(
        'Content-Type: application/json'
        );
    
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
        curl_setopt ( $ch, CURLOPT_URL, $url );       
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data);
        // Allow cUrl functions 20 seconds to execute
        curl_setopt ( $ch, CURLOPT_TIMEOUT, 20 );
        // Wait 10 seconds while trying to connect
        curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
        $output = array();
        $output['server_response'] = curl_exec( $ch );
        $curl_info = curl_getinfo( $ch );
        $output['http_status'] = $curl_info[ 'http_code' ];
        $output['error'] = curl_error($ch);
        curl_close( $ch );
        return $output;
    

    【讨论】:

      猜你喜欢
      • 2011-01-02
      • 2013-02-24
      • 2016-02-06
      • 2015-06-01
      • 2011-10-29
      • 2011-09-16
      • 2011-05-20
      • 2011-04-10
      • 1970-01-01
      相关资源
      最近更新 更多