簡體   English   中英

使用PHP CURL執行HTTP POST

[英]Execute a HTTP POST Using PHP CURL

我正在嘗試使用PHP CURL執行HTTP POST。

我得到的代碼不是用PHP編寫的,看起來像.net,但我從未編寫過代碼。 我基本上想將其轉換為PHP,以便根據需要更易於理解和編輯。

我想將所有內容發送到一個URL,然后將其在帶有附加令牌的iframe中打開。

// 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");
   }
}

據我所知,我已經使用curl和php嘗試過這種方法(有限)

// 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);

我的問題是,如何使用curl和php執行HTTP POST並將其回顯到iframe?

設法解決我的問題

<?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>

我將不做任何修改,以便其他人可以根據需要查看它的完成方式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM