Reputation: 329
This problem is confusing me. Here is my code:
try {
//API Url
$url = 'https:<REST OF URL>';
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = array(
'UserName' => '<HIDDEN>',
'Password' => '<HIDDEN>'
);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Execute the request
$content = curl_exec($ch);
catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
Im using a browser to run the php script and the output seems to be json format, which is what i need. However when i add json decode and try to print_r or var_dump the output, i simply get a boolean(true) or int(1), it doesnt want to take the data into an array. What is also odd and most likely related is that with the code above im not using print_r, or var_dump or even an echo and still it prints the json format to the screen?
Any help would be appreciated.
EDIT
Here is the json output, that appears on the screen:
{"AvailableAdvisers":[{"AdviserId":"1345678","BusinessEntityName":"Bob Smith Finance","FullName":"Bob Smith"},{"AdviserId":"12345678","BusinessEntityName":"Globe Home Loans Pty Ltd","FullName":"Jane Doe"}],"FirstName":"Sam","LastName":"Sung","LendingPartnerStaffId":"12345674356","Locations":[{"LendingPartnerLocationId":"123467867647","Name":"Bank"},{"LendingPartnerLocationId":"12324354545","Name":"Jane Smith"}],"UserName":"Username"}
Upvotes: 0
Views: 952
Reputation: 17289
ok, it seems I got your point.
So to get curl response into $content
variable you probably have to add line:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
before your curl_exec
But the miracle how the curl response appear on the page is real enigma :-)
Upvotes: 2