2012-11-14 73 views
2

我在Stack Overflow上查找了很多以找到对我的问题的答案,但根本不能。我试图发布以下JSON使用PHP cURL POST JSON并显示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; 
?> 

我没有得到任何回应,即使它可以正常使用jQuery和AJAX。当我检查Chrome的开发人员工具时,该方法是GET,这很奇怪,因为我在代码中将其设置为POST。

任何想法我做错了什么?

回答

-1

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; 
     } 
?> 
+0

什么......... – dbf

0

尽量让GET请求你JSON字符串作为请求正文:

$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; 
+0

我不知道它会与找份工作,因为我需要?! POST $ data_string,然后从服务器检索JSON响应。还是我在这里错了? – pshoeg

+0

这取决于服务器端。你说当你使用AJAX发出GET请求时,它对你有用。 – Eugene

0

你已经能够看看你的参数在接收端看起来像什么?

就Rookies的答案而言 - 它可能与你如何通过你的postfields有关吗?通常,postfield参数将期望关键值数组或urlencoded字符串(key1 = val1 &)。如果没有“JSON”($ data_string)“value”的“键”,服务器是否知道如何接受该后置字段?你可以试试以下吗?

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

祝你好运。