I am trying to Execute a HTTP POST Using PHP CURL.
I have been given code that is written in something that is not PHP,looks like .net but I have never coded in that. I basically want to convert it to PHP so that it will be easier to understand and edit as needed.
I want to send this all to a url that I will then open in an iframe with the token attached.
// The Cape Consumers token endpoint details. These should be configurable.
var tokenEndpoint = "https://webservices_test.capeconsumers.co.za/api/Utilities/StashData";
var endpointUser = "mylogin";
var endpointPassword = "mypass";
// The data to be sent to Cape Consumers. This can include any information required - e.g. ID number, Reality number, etc.
var data = new Dictionary<string, object>()
{
{ "User.IdentityNumber", "6405105007087" },
{ "CapeConsumers.TrackerNumber", "0E273247DB4840F6" },
{ "Call.AgentReference", "632" },
{ "Call.RecordingReference", "0211234567" }
};
// Prepare a web request to post the data.
var request = WebRequest.Create(tokenEndpoint);
// Use Basic HTTP authentication.
request.Credentials = new NetworkCredential(endpointUser, endpointPassword);
// Endpoint expects JSON to be POSTed.
request.Method = "POST";
request.ContentType = "application/json";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
var dataString = JsonConvert.SerializeObject(data).Dump("JSON sent to API");
writer.Write(dataString);
}
try
{
// Retrieve the response from the endpoint.
var response = request.GetResponse();
// Extract the token from the response.
var token = default(string);
using (var reader = new StreamReader(response.GetResponseStream()))
{
// The token is a JSON-serialized string, so remove the leading and triling quotation marks.
token = reader.ReadToEnd().Trim('"').Dump("Token from API");
}
// Inject the token into the LEAP URL as a query parameter.
// This base URL should also be configurable.
var url = "https://onlineapplicationtest.capeconsumers.co.za/Bridge/CapeConsumersSACommercial?token=" + token;
url.Dump("The URL to use to open LEAP in the IFrame.");
}
catch (WebException exception)
{
exception.Dump();
using (var content = new StreamReader(exception.Response.GetResponseStream()))
{
content.ReadToEnd().Dump("Content");
}
}
I have tried it like this, to the best of my knowledge with curl and php (limited)
// Use Basic HTTP authentication.
<?php
curl_setopt($process, CURLOPT_USERPWD, $endpointUser . ":" . $endpointPassword);
$process = curl_init($tokenEndpoint);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $endpointUser . ":" . $endpointPassword);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, $fields);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($process);
curl_close($process);
my question is, how does one use curl and php to Execute a HTTP POST and echo it into an iframe?