问题描述:

I've looked a lot around on Stack Overflow to find an answer to my issue, but simply can't. I'm trying to post the following JSON

<?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;

?>

I don't get any response, even though it works fine with jQuery and AJAX. When I check Chrome's developer tools, the method is GET, which is weird as I set it to POST in the code.

Any ideas what I'm doing wrong?

网友答案:

Try to make GET request with your JSON string as request body:

$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;
网友答案:

Have you been able to see what your args look like on the receiving end?

To the point of Rookies answer - could it have something to do with how you're passing your postfields? Generally the postfield param will expect key value arrays or urlencoded strings (key1=val1&). Without a "key" to your JSON ($data_string) "value" does the server know how to accept the postfields? Could you maybe try the following?

// 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...

Good luck.

网友答案:

I know the question is 2 years old but its still getting a lot of views.

This looks like a SSL problem. You could try:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

for more info read this: http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

网友答案:

CURLOPT_POSTFIELDS: must like this "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;
        }
?>
相关阅读:
Top