使用PHP CURL执行HTTP POST

时间:2016-01-20 06:53:38

标签: php curl

我正在尝试使用PHP CURL执行HTTP POST。

我得到的代码是用非PHP的东西编写的,看起来像.net,但我从未编写过。我基本上想将它转换为PHP,以便根据需要更容易理解和编辑。

我想将此全部发送到一个网址,然后我将在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中?

1 个答案:

答案 0 :(得分:0)

管理我的问题的答案

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

我将保留未经编辑的问题,以便其他任何人可以查看如果需要它是如何完成的