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

doukanhua0752
doukanhua0752 是的,根据http_build_query文档:“默认情况下,PHP_QUERY_RFC1738,它是根据RFC1738执行的编码...和application/x-www-form-urlencoded媒体类型,这意味着空格被编码为加号(+)。“
3 年多之前 回复
dongpan8439
dongpan8439 哦。真。我将删除urlencode并重试。
3 年多之前 回复
doumenshi1475
doumenshi1475 此外,您无需对用户名进行urlencode。
3 年多之前 回复
duanjiyun7391
duanjiyun7391 垃圾是登录页面,它说你没有登录,我相信(“呃iemismis......”)。这表明我的第一个OAuthcURL失败,或者您的第二阶段用户名/密码失败。我建议将命令行firstcurl的输出与第一阶段的PHP版本的输出进行比较。一旦两者相同,进入第二阶段。
3 年多之前 回复

1个回答



问题很可能是您的第一个请求失败导致您未正确设置SSL验证。 您必须为两个请求设置相同的CURL选项。 不同之处仅在于URL和POST数据。</ p>

因此代码应如下所示:</ p>

  $ cookie_file = public_path()。  '/session_cookies.txt'; 
nnif(!file_exists($ cookie_file))return;
$ pem_file_path ='/ applications / MAMP / conf / apache / opennid.benu.com.cert.pem';
nopoptions = 数组(
CURLOPT_RETURNTRANSFER =&gt; true,
CURLOPT_FOLLOWLOCATION =&gt; true,
CURLOPT_SSL_VERIFYPEER =&gt; true,
CURLOPT_CAINFO =&gt; $ pem_file_path,
CURLOPT_POST =&gt; true,
CURLOPT_COOKIEJAR =&gt; $ cookie_file,
CURLOPT_COOKIEFILE =&gt; $ cookie_file,
);
$ ch = curl_init('https://login.uat.site.be/openid/oauth/authorize');
curl_setopt_array($ ch,$ 选项);
curl_setopt($ ch,CURLOPT_POSTFIELDS,http_build_query(array(
'client_id'=&gt;'benu',
'state'=&gt; $ state,
'nonce'=&gt; $ nonce,\ n'claim'=&gt;'%7B%22id_token%22%3A%7B%22http%3A%2F%2Fsite.be%2Fclaims%2Froles%22%3Anull%7D%7D',
'response_type'=&gt; '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'=&gt;'manager-sitelogin@gmail.com',
'j_password'=&gt;'site1',
)));
$ result = curl_exec($ ch );
$ err = curl_errno($ ch);
curl_close($ ch);
if($ err){
echo'Error:'。 curl_error($ ch);
}
</ code> </ pre>
</ div>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐