【问题标题】:POST JSON to API and retrieve data with PHPPOST JSON 到 API 并使用 PHP 检索数据
【发布时间】:2016-02-19 08:35:30
【问题描述】:

我正在尝试使用 this API 并使用所需的参数向它发送 POST。

使用此代码:

function postToService_php( $urlAbs, $data ) {
    $dataJSON = json_encode($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $urlAbs);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJSON);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($dataJSON))                                                                       
    );
    $resultJSON = curl_exec($ch);
    return $resultJSON;
}

$postData = array(
    'UserName' => '*username*',
    'Password' => '*password*',
    'Area' => 1
);

var_dump(postToService_php('http://e20prodwebapi.esmartapi.com/api/Authenticate', $postData));

这似乎给了 string(0) "" 结果,我在这里做错了什么?

【问题讨论】:

  • 作为测试,尝试将$info=curl_getinfo( $ch );return $info; 添加到您的 curl 函数中,看看会显示什么。对我来说(没有用户名/密码)我收到了 403 响应
  • 我似乎得到了相同的响应,包括用户名和密码。编辑:根据“返回 200-OK 并提供有关用户的信息,如果用户在此区域(网络或电源)中不存在,则返回 401-未授权或 410-Gone。”如果用户不存在,它应该给出 410,对吧?
  • 我看不到注册方法,因此无法进一步测试 - 尽管使用 https 确实返回了 401
  • 太棒了,https 成功了!谢谢:)

标签: php json api curl


【解决方案1】:

此 API https 是必需的。我没有帐号,所以无法测试。

https://e20prodwebapi.esmartapi.com/api/Authenticate

【讨论】:

    【解决方案2】:

    使用这个:考虑您必须为此站点使用 SSL (HTTPS)

    <?php
    
    function postToService_php( $urlAbs, $data ) {
        $dataJSON = json_encode($data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $urlAbs);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJSON);                                                                  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1); //Remove this line if everything works okay.
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
        curl_setopt($ch,CURLOPT_POST, true);    
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
            'Content-Type: application/json',                                                                                
            'Content-Length: ' . strlen($dataJSON))                                                                       
        );
        $resultJSON = curl_exec($ch);
    
        return $resultJSON;
    }
    
    $postData = array(
        'UserName' => 'test',
        'Password' => 'test',
        'Area' => 1
    );
    
    var_dump(postToService_php('https://e20prodwebapi.esmartapi.com/api/Authenticate', $postData));
    

    【讨论】:

      【解决方案3】:

      预感我尝试了 https 并使用此代码收到了预期的 401 响应

      function postToService_php( $url, $data ) {
          $ch = curl_init();
          $data=json_encode( $data );
      
          curl_setopt($ch, CURLOPT_URL, $url );
      
          if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
              curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
              curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
              curl_setopt( $ch, CURLOPT_CAINFO, realpath( 'c:/wwwroot/cacert.pem' ) );
          }
      
          curl_setopt($ch, CURLOPT_POST, true );                                                                     
          curl_setopt($ch, CURLOPT_POSTFIELDS, $data );  
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );                                                               
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                     
          curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
              'Content-Type: application/json',
              'Content-Length: ' . strlen( $data ))                                                                       
          );
      
          $result = curl_exec( $ch );
          $info=curl_getinfo( $ch );
          curl_close( $ch );
      
          return (object)array(
              'result'=>  $result,
              'info'  =>  $info
          );
      }
      
      
      $url='https://e20prodwebapi.esmartapi.com/api/Authenticate';
      $postData = array(
          'UserName' => 'geronimo',
          'Password' => 'passthedoobie',
          'Area' => 1
      );
      
      print_r( postToService_php( $url, $postData ) );
      

      【讨论】:

        猜你喜欢
        • 2015-11-17
        • 2010-12-26
        • 1970-01-01
        • 1970-01-01
        • 2012-01-04
        • 1970-01-01
        • 2014-11-24
        • 2011-12-10
        • 1970-01-01
        相关资源
        最近更新 更多