【问题标题】:I can not post data using PHP我无法使用 PHP 发布数据
【发布时间】:2016-03-09 21:05:28
【问题描述】:

我使用了这段代码,但我总是得到空结果,我不知道为什么

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

【问题讨论】:

  • 我的猜测是因为至少有一个 post 变量没有设置。在您打开 <?php 标记 error_reporting(E_ALL); ini_set('display_errors', 1); 后立即将错误报告添加到文件顶部
  • 我建议研究 cURL 错误处理。
  • 我至少有 18% 的人认为不应执行 $str_data = json_encode($data);。尝试注释掉该行。
  • 尝试 curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER,false);

标签: php


【解决方案1】:

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

  1. CURL SSL 验证器设置为 false
  2. JSON 错误处理
  3. API 运行时异常
  4. 如果 api 状态为 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
    }
}
?>

请测试代码,如果需要任何修改,请告诉我

【讨论】:

  • 谢谢,我现在收到警告:http_build_query(): Parameter 1 expected to be Array or Object
  • 还有这个错误注意:未定义的索引:此行中的状态if ($apiResponse['status'] === 1) 如果我评论这些行我得到空 $result
  • 在注释了有错误的行后,我现在得到服务器的响应,谢谢
  • 上面的代码只是完成你的工作的协议......需要你的操作......很高兴听到代码有效。
猜你喜欢
  • 2014-10-05
  • 1970-01-01
  • 2021-02-27
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 2020-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多