PHP cURL Request for Web Service Returning Error -
i continue receive error: "failed de-serialize entity" when making rest request below. i'm assuming has formatting of json, can't life of me figure out. appreciated.
//login //set url login $url = $urlroot . "/session"; //build resource request $params = array("encryptedpassword" => false, "indentifier" => $username, "password" => $password); //json encode parameters $jsonparams = json_encode($params) ; //set 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 line production $result = curl_exec($ch); //output login response - includes header echo($result) . php_eol; //close curl resource free system resources curl_close($ch);
heres's link further details resource i'm trying consume https://labs.ig.com/rest-trading-api-reference/service-detail?id=124
i tried consume service in pure js alone. perhaps contrasting code make error in php obvious someone.
function login() { // username , password 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 login request, ie post request /session var req = new request(); req.method = "post"; req.url = urlroot + "/session"; // set standard request headers, i.e. api key, request content type (json), // , 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 request body user identifier (username) , password var bodyparams = {}; bodyparams["identifier"] = identifier; bodyparams["password"] = password; bodyparams["encryptedpassword"] = true; req.body = json.stringify(bodyparams); // prettify request display purposes $("#request_data").text(js_beautify(req.body) || ""); // send request via 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 , client session tokens, active account id, , lightstreamer endpoint, // these required 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 display purposes $("#response_data").text(js_beautify(data.responsetext) || ""); // show logged in status message on screen $("#loginstatus").css("color", "green").text("logged in " + accountid); }, error: function (response, status, error) { // login failed, because login id , password aren't correct handlehttperror(response); } }); } catch (e) { handleexception(e); } return true; }
json versioned. have make sure version encoding can decoded on other side.
below output of phpinfo();
on php 5.6.3 install:
there various json validation sites one: http://jsonlint.com/
Comments
Post a Comment