douhua1760 2016-03-09 21:05
浏览 23
已采纳

我无法使用PHP发布数据

I used this code but I get always empty result I don't know why

if (isset($_POST['uname'], $_POST['password'])){

      $uname = test_input($_POST['uname']);
      $pw = test_input($_POST['password']);

      $data = array(
          "loginEmail" => $uname,
          "loginPassword" => $pw,
          "clientURI" => "https://hk2.notify.windows.com/?token=AwYAAAC2bng45wHDpXWajWLTxGM1gxP1DcLlHTL8%2bhOfKVbkpYjl1U9tnUGrW4FSmwuiiWPKEyvSUoO5v9nfzFWZ097kdUeN8xDpAlp96Ilk3LCSN0sQNFuyU%3d"

      );

      $url_send ="https://dev-frontserver.com/api/login";
      $str_data = json_encode($data);
      $headers = array("Content-Type: application/json");

      function sendPostData($url, $post, $headers){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
          curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
          $result = curl_exec($ch);
          curl_close($ch);  
          return $result;
      }

      echo "result = " .sendPostData($url_send, $str_data, $headers);*/
}

Any idea what is wrong with my code ? I tried the api in https://www.hurl.it and it work but I can not get it to work in my code.

Here are the returned data from `curl_getinfo($ch);'

array (size=26)
  'url' => string 'https://dev-fronapp.com/api/login' (length=47)
  'content_type' => string 'application/json; charset=utf-8' (length=31)
  'http_code' => int 200
  'header_size' => int 268
  'request_size' => int 384
  'filetime' => int -1
  'ssl_verify_result' => int 20
  'redirect_count' => int 0
  'total_time' => float 2.14
  'namelookup_time' => float 0
  'connect_time' => float 0.344
  'pretransfer_time' => float 1.047
  'size_upload' => float 253
  'size_download' => float 3991
  'speed_download' => float 1864
  'speed_upload' => float 118
  'download_content_length' => float 3991
  'upload_content_length' => float 253
  'starttransfer_time' => float 2.125
  'redirect_time' => float 0
  'redirect_url' => string '' (length=0)
  'primary_ip' => string '' (length=13)
  'certinfo' => 
    array (size=0)
      empty
  'primary_port' => int 443
  'local_ip' => string '' (length=12)
  'local_port' => int 10484
  • 写回答

1条回答 默认 最新

  • dsdsm2016 2016-03-09 21:22
    关注

    Your modify code and changes made are

    1. CURL SSL verifier is set to false
    2. JSON error handling
    3. API runtime exceptions
    4. return values if api status is 1/200/ok/true.
        <?php
    if (isset($_POST['uname'], $_POST['password'])){
    
          $uname = test_input($_POST['uname']);
          $pw = test_input($_POST['password']);
    
          $data = array(
              "loginEmail" => $uname,
              "loginPassword" => $pw,
              "clientURI" => "https://hk2.notify.windows.com/?token=AwYAAAC2bng45wHDpXWajWLTxGM1gxP1DcLlHTL8%2bhOfKVbkpYjl1U9tnUGrW4FSmwuiiWPKEyvSUoO5v9nfzFWZ097kdUeN8xDpAlp96Ilk3LCSN0sQNFuyU%3d"
    
          );
    
          $url_send ="https://dev-frontserver.com/api/login";
          $str_data = json_encode($data);
          $headers = array("Content-Type: application/json");
    
          echo "result = " .sendPostData($url_send, $str_data, $headers);
    }
          function sendPostData($url, $post, $headers){
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
              curl_setopt($ch, CURLOPT_POST, 1);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
              //avoid checking ssl verifier
              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
              $result = curl_exec($ch);
              if($result === false) {
                throw new \RuntimeException(
                    'API call failed with cURL error: ' . curl_error($ch)
                );
                }
            curl_close($ch); 
            $apiResponse = json_decode($result, true);
    
        // Make sure we got back a well-formed JSON string and that there were no errors when decoding it
        $jsonError = json_last_error();
        if($jsonError !== JSON_ERROR_NONE) {
            throw new \RuntimeException(
                'API response not well-formed (json error code: ' . $jsonError . ')'
            );
        }
    
        // Print out the response details
        if($apiResponse['status'] === 1)//check field where 
         {
            // No errors encountered
            return $result;
        }
        else {
            // An error occurred
            die("Error:");//show error message from api
        }
    }
    ?>
    

    Please test code and let me know if any modifications needed

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,