简体   繁体   中英

Execute a HTTP POST Using PHP CURL

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?

Managed to work out an answer to my question

<?php
$endpointUser = "mylogin";
//
$endpointPassword = "mypass";
//
$url = "https://dataurl.com";

$iframeUrl =  "https://myurl.com?token=";

$fields = array(
    "User.IdentityNumber"=> $_GET['security_phrase'] ,
    "CapeConsumers.TrackerNumber"=> "6E273247DB4840G3",
    "Call.AgentReference"=> $_GET['user'] ,
    "Call.RecordingReference"=> $_GET['security_phrase']
);

$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERPWD, $endpointUser . ":" . $endpointPassword);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$token = curl_exec($process);
curl_close($process);
$token = str_replace('"','',$token);


?>


<iframe src="<?php echo $iframeUrl . $token; ?>" width="100%" height="800"></iframe>

I will leave the question unedited so that anybody else can have a look at how it was done if needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM