PHP中的HTTP POST

问题描述 投票:0回答:2

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

Shell上的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
2个回答
0
投票

似乎您在这里正确地完成了所有操作(除了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);

详细日志应该告诉您问题出在哪里,它说什么?

((您一开始也缺少<?php,并且您可能想在结尾添加var_dump($result);。为了加快处理速度,您可以添加CURLOPT_ENCODING=>''以使curl使用压缩进行传输,如果响应是可压缩的(例如JSON或HTML或文本),通常可以加快速度)]


0
投票

尝试此代码。

    $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;
© www.soinside.com 2019 - 2024. All rights reserved.