如何将数据发送到JSON Web服务?

时间:2018-07-10 08:27:34

标签: php json webservice-client

如何使用PHP执行curl命令?

curl -i -X GET -u user:userpass --url http://test/statistics.json \
-d '{"period":{"startdate":"YYYY-MM-DD+hh:mm:ss","enddate":"YYYY-MM-DD+hh:mm:ss"}}' \
-H 'Accept: application/json' -H 'Content-type: application/json'

我已经尝试过了。

$oar = '{"period":{"startdate":"2018-07-06+00:00:00","enddate":"2018-07-07+00:00:00"}}';
$ch = curl_init();

$service_url = $myURL;
//$content= json_decode("{'period':{'startdate':'2018-06-02+00:00:00','enddate':'2018-06-03+00:00:'0'}}");
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "fsd:fsdfsdfsdfdf");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_GET, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $oar);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);

var_dump($response);
echo $response;

但是只能从GET获得来自服务器的响应。 如果排除curl_setopt($curl, CURLOPT_POSTFIELDS, $oar);,则可以使用。但是随后该服务会忽略日期范围并设置默认范围。

-更新

这很好!感谢 Ray A

$service_url = "https://test/urk.json";
$data = array("period" => array("startdate"=>"2018-06-01+00:00:00","enddate"=>"2018-07-07+00:00:00"));
$data_string = json_encode($data);

  $curl= curl_init($service_url);
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($curl, CURLOPT_USERPWD, "dasd:asdas");
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'Content-Length: ' . strlen($data_string))
  );
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  $result = curl_exec($curl);
  echo $result;

4 个答案:

答案 0 :(得分:0)

初始化curl

$ch = curl_init('http://test/statistics.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

基本HTTP身份验证:

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'fsd:fsdfsdfsdfdf');

自定义标题:

curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Accept: application/json',
    'Content-Type: application/json'
]);

JSON有效载荷:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'period' => [
        'startdate' => '2018-07-06+00:00:00',
        'enddate' => '2018-06-03+00:00',
    ],
]));

执行请求:

$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);

if (!empty($error)) {
    die(sprintf('Response error: %s', $error));
}

$jsonResponse = json_decode($response, true);

if (json_last_error()) {
    die(sprintf('Malformed JSON received: %s', json_last_error_msg()));
}

print_r($jsonResponse);

答案 1 :(得分:0)

如果要通过HTTP在正文中发送数据,则不能使用GET HTTP方法。要在正文中发送数据,您可以使用其他HTTP方法,例如POST

如果排除了CURLOPT_POSTFIELDS选项,它起作用的原因是, 如果您设置CURLOPT_POST cURL 会自动将1设置为CURLOPT_POSTFIELDS。因此,您的GET请求成为POST请求。这就是为什么服务器仅接受GET 作为响应的原因。

参见此处:https://curl.haxx.se/libcurl/c/CURLOPT_POSTFIELDS.html

  

使用CURLOPT_POSTFIELDS意味着将CURLOPT_POST设置为1。

您使用的服务器仅接受不能在正文中发送JSON数据的GET(即您所说的话)。

但是,如果要通过GET而不是通过POST发送数据,请使用HTTP查询参数。
以下是有关W3School的一些信息。

答案 2 :(得分:0)

这很好!谢谢 Ray A

$service_url = "https://test/urk.json";
$data = array("period" => array("startdate"=>"2018-06-01+00:00:00","enddate"=>"2018-07-07+00:00:00"));
$data_string = json_encode($data);

  $curl= curl_init($service_url);
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($curl, CURLOPT_USERPWD, "dasd:asdas");
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'Content-Length: ' . strlen($data_string))
  );
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  $result = curl_exec($curl);
  echo $result;

答案 3 :(得分:-3)

尝试遵循以下代码:

    $service_url = $myURL;
    $data = array("period" => array("startdate"=>"2018-07-06+00:00:00","enddate"=>"2018-07-07+00:00:00"));                                                                    
    $data_string = json_encode($data);                                                                                                                                                                                                        
    $curl= curl_init($service_url);  
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "fsd:fsdfsdfsdfdf");                                                                    
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    );                                                                                                                   
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);                                                                                                           
    $result = curl_exec($curl);