我无法使用PHP发布数据

时间:2016-03-09 21:05:28

标签: 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");

      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);*/
}

知道我的代码有什么问题吗?我在https://www.hurl.it中尝试了api并且它可以工作,但我无法在我的代码中使用它。

以下是来自`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 个答案:

答案 0 :(得分:0)

您的修改代码和所做的更改是

  
      
  1. CURL SSL验证程序设置为false
  2.   
  3. JSON错误处理
  4.   
  5. API运行时异常
  6.   
  7. 如果api状态为1/200 / ok / true则返回值。
  8.   
    <?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
    }
}
?>

请测试代码并告知我是否需要进行任何修改