【问题标题】:Converting cUrl PHP code to coldfusion将 cUrl PHP 代码转换为冷融合
【发布时间】:2015-12-07 05:51:40
【问题描述】:

我正在尝试将一些 PHP 代码转换为 ColdFusion 并在 curl 方法中遇到问题。

PHP

  // Password
    $clientSecret = urlencode(settings::$password);
    // Information about the resource we need access for which in this case is graph.
    $graphId = 'https://graph.windows.net';
    $protectedResourceHostName = 'graph.windows.net';
    $graphPrincipalId = urlencode($graphId);
    // Information about the app
    $clientPrincipalId = urlencode($appPrincipalId);

    // Construct the body for the STS request
    $authenticationRequestBody = 'grant_type=client_credentials&client_secret='.$clientSecret
              .'&'.'resource='.$graphPrincipalId.'&'.'client_id='.$clientPrincipalId;

    //Using curl to post the information to STS and get back the authentication response    
    $ch = curl_init();
    // set url 
    $stsUrl = 'https://login.windows.net/'.$appTenantDomainName.'/oauth2/token?api-version=1.0';        
    curl_setopt($ch, CURLOPT_URL, $stsUrl); 
    // Get the response back as a string 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    // Mark as Post request
    curl_setopt($ch, CURLOPT_POST, 1);
    // Set the parameters for the request
    curl_setopt($ch, CURLOPT_POSTFIELDS,  $authenticationRequestBody);

    // By default, HTTPS does not work with curl.
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    // read the output from the post request
    $output = curl_exec($ch);         
    // close curl resource to free up system resources
    curl_close($ch);   

ColdFusion代码:

    <cfhttp url="#stsUrl#" method="POST" result="resultName">
        <cfhttpparam type="formfield" name="grant_type" value="client_credentials">
        <cfhttpparam type="formfield" name="client_secret" value="#clientSecret#">
        <cfhttpparam type="formfield" name="resource" value="#graphPrincipalId#">
        <cfhttpparam type="formfield" name="client_id" value="#clientPrincipalId#">
   </cfhttp>

运行 cfhttp 调用时,我收到了 400 Bad Request 错误。

我错过了什么吗?

【问题讨论】:

  • 你如何设置authenticationRequestBody?你可以在你的帖子中添加它吗?另外,要添加数据到请求正文类型必须是body
  • @Beginner :添加了更多详细信息。所以你认为 httpparams 需要是body 类型而不是formfied
  • httpparams need to be of type body instead of formfied? 您正在尝试将数据添加到请求正文。所以它应该是 body 并且您需要为此手动设置 content-type 标头。文档:cfhttpparam.

标签: php curl coldfusion coldfusion-9 cfhttp


【解决方案1】:

我错过了什么吗?

  1. 要向HTTP请求的body添加数据,您需要将cfhttpparamtype设置为body
  2. 您需要为正文中的内容类型手动设置Content-Type 标头。

所以,你可以试试这个:

<!--- Set defaults  --->
<cfset requestBody = "">
<cfset stsUrl = "https://login.windows.net/#appTenantDomainName#/oauth2/token?api-version=1.0">

<!--- Set Data Variables --->
<cfset data["grant_type"] = "client_credentials">
<cfset data["client_secret"] = clientSecret>
<cfset data["resource"] = graphPrincipalId>
<cfset data["client_id"] = clientPrincipalId>

<!--- Request Body --->
<cfloop collection="#data#" item="key">
    <cfset requestBody &= key & "=" & data[key] & "&">
</cfloop>
<cfset requestBody = reReplace(requestBody, "&$", "", "1")>

<!--- Request --->
<cfhttp url="#stsUrl#" method="POST" result="resultName">       
    <cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
    <cfhttpparam type="body" value="#requestBody#">
</cfhttp>

与问题无关:您必须正确确定变量的范围。

【讨论】:

    【解决方案2】:

    感谢@Beginner 指导我。以下解决方案有效:

    <cfset authenticationRequestBody = "grant_type=client_credentials&client_secret=#clientSecret#&resource=#graphPrincipalId#&client_id=#clientPrincipalId#">
    <cfset stsUrl = "https://login.windows.net/#appTenantDomainName#/oauth2/token?api-version=1.0">
    <cfhttp url="#stsUrl#" method="POST" result="resultName">       
        <cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
        <cfhttpparam type="body" value="#authenticationRequestBody#">
    </cfhttp>
    

    【讨论】:

    • 感谢您发布工作代码。但是,如果@Beginner 提供了解决方案,请他们将他们的评论推广到官方“答案”(并将其标记为解决方案)可能是一个很好的姿态,以感谢您的帮助:-)
    • @Leigh :初学者仍然可以发表他的评论作为答案。我已经支持了他的评论并在我的工作解决方案中提到了他。
    • {给 Bob Barker 留下最好的印象} @Beginner 下来... ;-)
    猜你喜欢
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 2011-07-02
    • 2019-02-11
    • 2014-08-28
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    相关资源
    最近更新 更多