使用PHP cURL发布JSON并显示JSON响应
为了找到我的问题的答案,我在堆栈溢出问题上做了很多研究,但就是找不到答案。我试图发布以下JSON使用PHP cURL发布JSON并显示JSON响应,php,json,curl,Php,Json,Curl,为了找到我的问题的答案,我在堆栈溢出问题上做了很多研究,但就是找不到答案。我试图发布以下JSON <?php $data_string = '{ "jsonrpc": "2.0", "method": "login", "id": 1, "params": { "params": { "username": "4321", "password": "123
<?php
$data_string = '{
"jsonrpc": "2.0",
"method": "login",
"id": 1,
"params": {
"params": {
"username": "4321",
"password": "1234"
}
}
}';
$ch = curl_init('https://domain.com');
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);
echo $result;
?>
我没有得到任何回应,即使它可以与jQuery和AJAX配合使用。当我检查Chrome的开发者工具时,方法是GET,当我将它设置为POST时,这很奇怪
知道我做错了什么吗?CURLOPT_POSTFIELDS:一定是这样a=1111&b=2222
example 1:
<?php
$useragent = 'PHP Client 1.0 (curl) ' . phpversion();
$post_string="a=1&b=1";
$url_with_get="http://xxx.xxx.com";
$result = @exec("curl -s --connect-timeout 10 --user-agent \"$useragent\" -d\"$post_string\" \"$url_with_get\"");
var_dump($result);
?>
example 2:
<?php
$useragent = 'PHP Client 1.0 (curl) ' . phpversion();
$post_string="a=1&b=1";
$url_with_get="http://xxx.xxx.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_with_get);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
?>
example 3:
<?php
$content_type = 'application/x-www-form-urlencoded';
$content = "a=1&b=1";
$server_addr = "http://xxx.xxx.com";
var_dump(http_post($content_type, $content, $server_addr));
function http_post($content_type, $content, $server_addr) {
$user_agent = 'PHP Client 1.0 (non-curl) ' . phpversion();
$content_length = strlen($content);
$context = array(
'http' => array(
'method' => 'POST',
'user_agent' => $user_agent,
'header' => 'Content-Type: ' . $content_type . "\r\n" .
'Content-Length: ' . $content_length,
'content' => $content,
'timeout' => 10,
)
);
$context_id = stream_context_create($context);
$sock = fopen($server_addr, 'r', false, $context_id);
$result = '';
if ($sock) {
while (!feof($sock)) {
$result .= fgets($sock, 4096);
}
fclose($sock);
}
return $result;
}
?>
CURLOPT_POSTFIELDS:必须像这样a=1111&b=2222
example 1:
<?php
$useragent = 'PHP Client 1.0 (curl) ' . phpversion();
$post_string="a=1&b=1";
$url_with_get="http://xxx.xxx.com";
$result = @exec("curl -s --connect-timeout 10 --user-agent \"$useragent\" -d\"$post_string\" \"$url_with_get\"");
var_dump($result);
?>
example 2:
<?php
$useragent = 'PHP Client 1.0 (curl) ' . phpversion();
$post_string="a=1&b=1";
$url_with_get="http://xxx.xxx.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_with_get);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
?>
example 3:
<?php
$content_type = 'application/x-www-form-urlencoded';
$content = "a=1&b=1";
$server_addr = "http://xxx.xxx.com";
var_dump(http_post($content_type, $content, $server_addr));
function http_post($content_type, $content, $server_addr) {
$user_agent = 'PHP Client 1.0 (non-curl) ' . phpversion();
$content_length = strlen($content);
$context = array(
'http' => array(
'method' => 'POST',
'user_agent' => $user_agent,
'header' => 'Content-Type: ' . $content_type . "\r\n" .
'Content-Length: ' . $content_length,
'content' => $content,
'timeout' => 10,
)
);
$context_id = stream_context_create($context);
$sock = fopen($server_addr, 'r', false, $context_id);
$result = '';
if ($sock) {
while (!feof($sock)) {
$result .= fgets($sock, 4096);
}
fclose($sock);
}
return $result;
}
?>
尝试使用JSON字符串作为请求正文发出GET请求:
$data_string = '{
"jsonrpc": "2.0",
"method": "login",
"id": 1,
"params": {
"params": {
"username": "4321",
"password": "1234"
}
}
}';
$ch = curl_init('https://domain.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$result = curl_exec($ch);
echo $result;
尝试使用JSON字符串作为请求正文发出GET请求:
$data_string = '{
"jsonrpc": "2.0",
"method": "login",
"id": 1,
"params": {
"params": {
"username": "4321",
"password": "1234"
}
}
}';
$ch = curl_init('https://domain.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$result = curl_exec($ch);
echo $result;
你能看到你的args在接收端的样子吗 新秀们的回答是:这可能与你如何通过季后赛有关吗?通常,postfield参数需要键值数组或urlencoded字符串key1=val1&。如果没有JSON$data_字符串值的键,服务器是否知道如何接受postfields?你可以试试下面的吗
// Personal preference here - arrays are easier for me to read
// Create a multi dem array dictionary with your values
$_dictionary = array("jsonrpc"=>"2.0",
"method" =>"login",
"id" =>1,
"params" =>array("params"=>array("username"=>"4321","password"=>"1234"))
);
// json_encode
$_dictionary = json_encode($_dictionary);
// your $data_string variable will now be in key=value
$data_string = "mydata={$_dictionary}";
// set $data_string to your CURLOPT_POSTFIELDS...
祝你好运 你能看到你的args在接收端的样子吗 新秀们的回答是:这可能与你如何通过季后赛有关吗?通常,postfield参数需要键值数组或urlencoded字符串key1=val1&。如果没有JSON$data_字符串值的键,服务器是否知道如何接受postfields?你可以试试下面的吗
// Personal preference here - arrays are easier for me to read
// Create a multi dem array dictionary with your values
$_dictionary = array("jsonrpc"=>"2.0",
"method" =>"login",
"id" =>1,
"params" =>array("params"=>array("username"=>"4321","password"=>"1234"))
);
// json_encode
$_dictionary = json_encode($_dictionary);
// your $data_string variable will now be in key=value
$data_string = "mydata={$_dictionary}";
// set $data_string to your CURLOPT_POSTFIELDS...
祝你好运 我知道这个问题已经提了2年了,但它仍然得到了很多意见 这看起来像是SSL问题。你可以试试:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
欲了解更多信息,请阅读以下内容:我知道这个问题已经讨论了2年,但仍然有很多人对它发表了看法 这看起来像是SSL问题。你可以试试:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
有关更多信息,请阅读以下内容:我不确定它是否能与GET一起使用,因为我需要发布$data\u字符串,然后从服务器检索JSON响应。还是我错了?这取决于服务器端。您说过,当您使用AJAX发出GET请求时,它对您有效。我不确定它是否适用于GET,因为我需要发布$data\u字符串,然后从服务器检索JSON响应。还是我错了?这取决于服务器端。您说过,当您使用AJAX发出GET请求时,它对您有效。