PHP cURL请求Web服务返回错误

时间:2015-06-04 09:40:31

标签: php curl

我继续收到错误:"无法反序列化实体"在下面发出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;

}

1 个答案:

答案 0 :(得分:0)

JSON已版本化。您必须确保您编码的版本可以在另一侧进行解码。

以下是我的PHP 5.6.3安装上phpinfo();的输出:

enter image description here

还有各种类似JSON的验证网站:http://jsonlint.com/