douhui9380 2017-01-25 15:22
浏览 80

将cURL命令转换为PHP cURL(SSL)

I ran

this curl in the command line in my Terminal

curl -c session_cookies.txt "https://login.uat.site.be/openid/oauth/authorize?client_id=site&state=e1968018-bb04-4475-86fa-b0728e9fb038&nonce=f7b76b29-2c42-423d-8d1c-5b1d0a8ce8bf&response_type=code&claims=%7B%22id_token%22%3A%7B%22http%3A%2F%2Fsite.be%2Fclaims%2Froles%22%3Anull%7D%7D" 2>/dev/null| curl -v -b session_cookies.txt -L -H "Content-Type: application/x-www-form-urlencoded" -v -d 'j_username=manager-sitelogin@gmail.com&j_password=123' "https://login.uat.site.be/openid/login.do" 2>&1 >/dev/null | grep Location | grep code=

I got

this response back

< Location: https://testserver.sitenets.com/user/secure/dashboard?code=4z71kBZ4iYlqfDB7qOHvZgyXkqVEaL7v&state=e1968018-bb04-4475-86fa-b0728e9fb038


Note

code=4z71kBZ4iYlqfDB7qOHvZgyXkqVEaL7v

That is what I am after.


Then

I’ve tried converting my curl command lines to PHP curl

  $ch = curl_init( 'https://login.uat.site.be/openid/oauth/authorize' );
  curl_setopt($ch, CURLOPT_POST, true );
  curl_setopt($ch, CURLOPT_POSTFIELDS, array(

    'client_id' => 'site',
    'state' => $state,
    'nonce' => $nonce,
    'claims' => '%7B%22id_token%22%3A%7B%22http%3A%2F%2Fsite.be%2Fclaims%2Froles%22%3Anull%7D%7D',
    'response_type' => 'code'

    ) );

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_COOKIEJAR, public_path().'/session_cookies.txt');
  curl_setopt($ch, CURLOPT_VERBOSE, true);

  $first_curl = curl_exec($ch);

  $cookie_file = public_path(). '/session_cookies.txt';
  if (file_exists($cookie_file)) {

    $pem_file_path = '/Applications/MAMP/conf/apache/openid.site.com.cert.pem';

    $options = array(
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_SSL_VERIFYPEER => true,
      CURLOPT_CAINFO=> $pem_file_path,
      CURLOPT_POST => true,
      CURLOPT_POSTFIELDS => http_build_query(array(
        'j_username' => urlencode($un),
        'j_password' => $pw,
        )),
      CURLOPT_COOKIEJAR => $cookie_file,
      CURLOPT_COOKIEFILE => $cookie_file,
      );
    $ch = curl_init();
    curl_setopt_array($ch, $options);
    curl_setopt($ch, CURLOPT_URL, 'https://login.uat.site.be/openid/login.do');
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
      echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);

    dd($result);
  }

I never get the same result as I would have execute that command on my Terminal.

I keep getting this junk on my browser, when I dd out my $result variable

   """
    

    

    

    

    

    

    <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->

    <!DOCTYPE html>

    <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->

    <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->

    <!--[if gt IE 8]><!-->

    <html class="no-js">

    <!--<![endif]-->

    <head>

        <title>site: Meld je aan</title>

        <meta content="IE=edge" http-equiv="X-UA-Compatible">

        <meta charset="utf-8">

        <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">

        <meta name="description" content="">

    

        <link rel="shortcut icon" href="//static.site.be/assets/favicon/favicon.ico" type="image/x-icon"/>

        <link rel="stylesheet" href="css/vendor/normalize.min.css">

        <link rel="stylesheet" href="css/login.css">

        <link rel="stylesheet" type="text/css" href="//static.site.be/assets/fonts/omnes-breuer-logo/stylesheet.css">

        

        <link rel="stylesheet" href="https://static.uat.site.be/oauth2/css/main.css">

    

        <!-- Adobe analytics -->

        <script src="https://www2.site.be/etc/designs/site-be/js/statistics/SiteCatalyst.js"></script>

    

        <script type="text/javascript">

            function hideHtmlElement(id) {

                document.getElementById(id).style.display = "none";

            }

    

            function showHtmlElement(id) {

                document.getElementById(id).style.display = "block";

            }

    

            function showPanel(id) {

                hideHtmlElement("forgotLogin");

                hideHtmlElement("showLogin");

                hideHtmlElement("forgotPassword");

                showHtmlElement(id);

            }

    

        </script>

    

        <!--[if lt IE 9]>

        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>

        <![endif]-->

    </head>

    

    

    

    

    

    <div class="wrapper">

        <span id="showBackgroundForStatic" style="display: none">true</span>

        <!-- ERROR SCREEN -->

        <div class="container">

            <div id="loginContainer">

                <div id="showLogin">

                    <div class="options clearfix">

                        <div class="option-sites">

                            <ul>

                                <li><a target="_blank"

                                       href="http://site.be/nl">Meer site</a></li>

                                <li><a target="_blank" href="http://site.be/nl"

                                       tabindex="-1">site.be</a></li>

                                <li><a target="_blank" href="http://business.site.be/"

                                       tabindex="-1">Business</a></li>

                                <li><a target="_blank"

                                       href="http://corporate.site.be/" tabindex="-1">Over site</a></li>

                                <li><a target="_blank"

                                       href="http://klantenservice.site.be/"

                                       tabindex="-1">Klantenservice</a></li>

                            </ul>

                        </div>

                        <div class="option-language">

                            <ul>

                                <span id="languageForStatic" style="display: none">nl</span>

                                <!-- IT-4522 The design team uses languageForStatic to display text dynamically-->

                                

                                    

                                        <li class="active"><a href="?lang=nl" tabindex="-1">NL</a></li>

                                        <li><a href="?lang=fr" tabindex="-1">FR</a></li>

                                    

                                    

                                

                            </ul>

                        </div>

                    </div>

    

                    <div class="branding clearfix">

                        <div id="logo"><img src="https://static.site.be/oauth2/images/logosite.png"

                                            data-at2x="https://static.site.be/oauth2/images/logosite.png@2x.png"

                                            alt="site"></div>

                        <h1><label>Fout bij aanmelden</label></h1>

                        <span id="languageForStatic" style="display: none">nl</span>

                        <!-- IT-5252 The design team uses languageForStatic to display text dynamically-->

                    </div>

                    <div class="contentContainer">

                        <p>Er ging iets mis. Gelieve de applicatie af te sluiten en daarna opnieuw op te starten.</p>

                    </div>

                </div>

    

    

            </div>

            <div class="copyright">&copy; site

                <script>document.write(new Date().getFullYear());</script>

                - <a href="http://klantenservice.site.be/content/juridische-informatie" target="_blank"

                     tabindex="-1">Juridische informatie</a>

                - <a href="https://www2.site.be/nl/privacy/"

                     href="https://www2.site.be/nl/privacy/" target="_blank"

                     tabindex="-1">Privacy</a><br>

                <a href="http://klantenservice.site.be/content/waar-vind-ik-de-algemene-voorwaarden-van-site" target="_blank"

                   tabindex="-1">Tariefinfo en algemene voorwaarden</a>

            </div>

        </div>

        <div class="bgContainer"></div>

    </div>

    

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

    <script src="/openid/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>

    <script src="/openid/js/vendor/retina-1.1.0.min.js"></script>

    

    <!--[if lt IE 9]>!

    <script type="text/javascript" src="/openid/js/vendor/jquery.backgroundSize.js"></script>!

    <![endif]-->

    

    <script src="https://static.uat.site.be/oauth2/js/items.js"></script>

    <script src="https://static.uat.site.be/oauth2/js/main.js"></script>

    

    </body>

</html>

"""

I am stuck now, I am not sure what I did wrong, I am not sure what to do next.


Questions

How can I convert my curl to get the same result?

Is it something that I need to enable on my PHP curl settings?

Is it something that I need to update on my Apache settings?

What is wrong with my PHP CURLs?

How would one go about and debug this further?

  • 写回答

1条回答 默认 最新

  • dony39517 2017-01-26 18:44
    关注

    Most likely the problem is that your first request fails cause you don't setup SSL verifying properly. You have to set the same CURL options for both requests. The difference is only URL and POST data.

    So the code should look like this:

    $cookie_file = public_path(). '/session_cookies.txt';
    if (!file_exists($cookie_file)) return;
    $pem_file_path = '/Applications/MAMP/conf/apache/openid.benu.com.cert.pem';
    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_SSL_VERIFYPEER => true,
        CURLOPT_CAINFO=> $pem_file_path,
        CURLOPT_POST => true,
        CURLOPT_COOKIEJAR => $cookie_file,
        CURLOPT_COOKIEFILE => $cookie_file,
    );
    $ch = curl_init('https://login.uat.site.be/openid/oauth/authorize');
    curl_setopt_array($ch, $options);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
        'client_id' => 'benu',
        'state' => $state,
        'nonce' => $nonce,
        'claims' => '%7B%22id_token%22%3A%7B%22http%3A%2F%2Fsite.be%2Fclaims%2Froles%22%3Anull%7D%7D',
        'response_type' => 'code'
    )));
    $result = curl_exec($ch);
    $err = curl_errno($ch);
    curl_close($ch);
    if ($err)  {
      echo 'Error:' . curl_error($ch); return;
    }
    $ch = curl_init('https://login.uat.site.be/openid/login.do');
    curl_setopt_array($ch, $options);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
        'j_username' => 'manager-sitelogin@gmail.com',
        'j_password' => 'site1',
    )));
    $result = curl_exec($ch);
    $err = curl_errno($ch);
    curl_close($ch);
    if ($err) {
      echo 'Error:' . curl_error($ch);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3