douyun1860 2016-01-20 06:53
浏览 40
已采纳

使用PHP CURL执行HTTP POST

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?

  • 写回答

1条回答 默认 最新

  • duanmaduan1848 2016-01-20 08:38
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀