簡體   English   中英

Web服務的PHP cURL請求返回錯誤

[英]PHP cURL Request for Web Service Returning Error

我在下面發出REST請求時繼續收到錯誤:“無法反序列化實體”。 我以為它與json的格式有關,但我一生都無法弄清楚。 任何幫助將非常感激。

//LOGIN
//Set the url for login
$url = $urlRoot . "/session";

//Build the resource request
$params = array("encryptedPassword" => false, "indentifier" => $userName, "password" => $password);

//JSON encode parameters
$jsonParams = json_encode($params) ;

//Set up the curl resource
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json; charset=UTF-8",
    "Accept: application/json; charset=UTF-8", 
    "X-IG-API-KEY: ".$apiKey,
    "Version: 1"
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonParams);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //**NB delete this line for production

$result = curl_exec($ch);

//Output login response - includes the header
echo($result) . PHP_EOL;

//Close curl resource to free up system resources
curl_close($ch);

這是一個鏈接,該鏈接進一步詳細說明了我要消耗的資源https://labs.ig.com/rest-trading-api-reference/service-detail?id=124

我嘗試僅在純JS中成功使用服務。 也許對比一下代碼會使我在PHP中的錯誤對某些人顯而易見。

function login() {

   // Get username and password from user interface fields
   apiKey = $("#apikey").val();
   var identifier = $("#username").val();
   var password = $("#password").val();

   if (apiKey=="" || identifier=="" || password=="") {
       return false;
   }

   password = encryptedPassword(password);
   console.log("Encrypted password " + password);

   // Create a login request, ie a POST request to /session
   var req = new Request();
   req.method = "POST";
   req.url = urlRoot + "/session";

   // Set up standard request headers, i.e. the api key, the request content type (JSON), 
   // and the expected response content type (JSON)
   req.headers = {
      "Content-Type": "application/json; charset=UTF-8",
      "Accept": "application/json; charset=UTF-8",
      "X-IG-API-KEY": apiKey,
      "Version": "2"
   };

   // Set up the request body with the user identifier (username) and password
   var bodyParams = {};
   bodyParams["identifier"] = identifier;
   bodyParams["password"] = password;
   bodyParams["encryptedPassword"] = true;
   req.body = JSON.stringify(bodyParams);

   // Prettify the request for display purposes only
   $("#request_data").text(js_beautify(req.body) || "");

   // Send the request via a Javascript AJAX call
   try {
      $.ajax({
         type: req.method,
         url: req.url,
         data: req.body,
         headers: req.headers,
         async: false,
         mimeType: req.binary ? 'text/plain; charset=x-user-defined' : null,
         success: function (response, status, data) {

            // Successful login 
            // Extract account and client session tokens, active account id, and the Lightstreamer endpoint,
            // as these will be required for subsequent requests
            account_token = data.getResponseHeader("X-SECURITY-TOKEN");
            console.log("X-SECURITY-TOKEN: " + account_token);
            client_token = data.getResponseHeader("CST");
            console.log("CST: " + client_token);
            accountId = response.currentAccountId;
            lsEndpoint = response.lightstreamerEndpoint;

            // Prettify response for display purposes only
            $("#response_data").text(js_beautify(data.responseText) || "");

            // Show logged in status message on screen
            $("#loginStatus").css("color", "green").text("Logged in as " + accountId);
         },
         error: function (response, status, error) {

            // Login failed, usually because the login id and password aren't correct
            handleHTTPError(response);
         }
      });
   } catch (e) {
      handleException(e);
   }

    return true;

}

JSON已版本化。 您將必須確保編碼的版本可以在另一側解碼。

以下是phpinfo();的輸出phpinfo(); 在我的PHP 5.6.3安裝中:

在此處輸入圖片說明

也有許多類似這樣的JSON驗證站點: http : //jsonlint.com/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM