Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中的HTTP POST_Php_Curl_Post_Http Post_Php Curl - Fatal编程技术网

PHP中的HTTP POST

PHP中的HTTP POST,php,curl,post,http-post,php-curl,Php,Curl,Post,Http Post,Php Curl,我试图使用PHP发布JSON负载,但无法使其正常工作。我有下面的CURL命令可以在Shell上运行,还有下面的Python代码也可以运行,但是需要PHP实现相同的功能 壳上的卷曲: curl -H "Content-Type: application/json" -X POST -d '{"Content":" <CONTENT OF THE MESSAGE> "}' <URL of the node receiving POST data> curl-H“内容类型:a

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

壳上的卷曲:

curl -H "Content-Type: application/json" -X POST -d '{"Content":" <CONTENT OF THE MESSAGE> "}' <URL of the node receiving POST data>
curl-H“内容类型:application/json”-X POST-d'{“内容”:“}”
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)
#/usr/bin/python
#-*-编码:utf-8-*-
导入请求
标题={
“内容类型”:“应用程序/json”,
}
auth=''
第1行=“”
数据=“{”内容“:“%s”}%”(第1行)
response=requests.post(url=auth,headers=headers,data=data)
到目前为止,我对PHP的了解(不起作用):

$data=array(“Content”=>”);
$data\u string=json\u encode($data);
$ch=curl_init(“”);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,“POST”);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,数组(
'内容类型:application/json',
“内容长度:”.strlen($data_string))
);                                                                                                                   
$result=curl\u exec($ch);

任何和所有的帮助,请提前感谢

看起来您在那里做的每件事都是正确的(除了第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);
详细日志应该告诉您问题是什么,它说了什么

(另外,您缺少
请尝试此代码

    $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;
$ch=curl_init();
$data=数组(“内容”=>”);
$headers=数组(
'内容类型:应用程序/json'
);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_URL,$URL);
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
//允许cUrl函数执行20秒
curl_setopt($ch,CURLOPT_超时,20);
//尝试连接时请等待10秒钟
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
$output=array();
$output['server\u response']=curl\u exec($ch);
$curl\u info=curl\u getinfo($ch);
$output['http_status']=$curl_info['http_code'];
$output['error']=curl_error($ch);
卷曲关闭($ch);
返回$output;

什么是实际的错误消息,或者什么具体的错误消息不起作用?它基本上是一个消息应用程序的webhook…在运行PHP文件后,什么都不会发生,而其他两个“发布”消息。这是否回答了您的问题?请从curl cmdline中删除
-X post
部分。。。
    $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;