将cURL命令翻译成PHP cURL(SSL) [英] Translate cURL commands into PHP cURL (SSL)

查看:206
本文介绍了将cURL命令翻译成PHP cURL(SSL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的终端的命令行中运行



curl -c session_cookies.txthttps://login.uat.site.be/openid/oauth/authorize?client_id=benu&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%7D2 / dev / null | curl -v -b session_cookies.txt -L -HContent-Type:application / x-www-form-urlencoded-v -d'j_username=manager-sitelogin@gmail.com& j_password = 123'https: /login.uat.site.be/openid/login.do2>& 1> / dev / null | grep位置| grep code =



我收到了



b
$ b

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








code = 4z71kBZ4iYlqfDB7qOHvZgyXkqVEaL7v p>

这是我之后。






然后



我试过将curl命令行转换为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'=>'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'

));

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

我从来没有得到与我在终端上执行该命令相同的结果。



当我 dd 出现在我的 $结果变量

 
\r\\\

\r\\\

\r\\\

\r\\\

\r\\\

\r\\\

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

<!DOCTYPE html> \r\\\

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

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

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

< html class =no-js> \r\\\

<! - <![endif] - > \r\\\

< head> r \\\

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

< meta content =IE = edgehttp-equiv =X-UA- Compatible> \r\\\

< meta charset =utf-8> \r\\\

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

< meta name =descriptioncontent => \r\\\

\r\\\

< link rel =shortcut iconhref =// static.site.be/assets/favicon/favicon.icotype =image / x-icon/> ; \r\\\

< link rel =stylesheethref =css / vendor / normalize.min.css> \r\\\

< link rel = stylesheethref =css / login.css> \r\\\

< link rel =stylesheettype =text / csshref =// static.site.be/assets /fonts/omnes-breuer-logo/stylesheet.css\"> ;\r\\\

\r\\\

< link rel =stylesheethref =https:// static.uat.site.be/oauth2/css/main.css\"> \r\\\

\r\\\

<! - Adob​​e analytics - > \\ \\ r \\\

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

\r\\\

< script type =text / javascript> \r\\\

function hideHtmlElement(id){\r\\ \\ n
document.getElementById(id).style.display =none; \r\\\

} \r\\\

\r\\\

function showHtmlElement(id){\r\\\

document.getElementById(id).style.display =block; \r\\\

} \r\\\

\r\\\

function showPanel(id){\r\\\

hideHtmlElement(forgotLogin); \r\\\

hideHtmlElement showLogin); \r\\\

hideHtmlElement(forgotPassword); \r\\\

showHtmlElement(id); \r\\\

} \r \\\

\r\\\

< / script> \r\\\

\r\\\

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

< script src =http://html5shiv.googlecode.com/svn/trunk/html5.js>< / script> \r\\ \\ n
<![endif] - > \r\\\

< / head> \r\\\

\r\\\

\r\\\

\r\\\

\r\\\

\r\\\

< div class =wrapper> ; \r\\\

< span id =showBackgroundForStaticstyle =display:none> true< / span> \r\\\

<! - ERROR SCREEN - > \r\\\

< div class =container> \r\\\

< div id =loginContainer> \r\\\

< div id =showLogin> \r\\\

< div class =options clearfix> \r\\\

< div class = option-sites> \r\\\

< ul> \r\\\

< li>< a target =_ blank\r\\\

href =http://site.be/nl> Meer网站< / a>< / li> \r\\\

< li>< a target =_ blank href =http://site.be/nl\r\\\

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

< li>< a target =_ blankhref =http://business.site.be/\r\\\

tabindex = - 1> / a>< / li> \r\\\

< li>< a target =_ blank\r\\\

href =http://corporate.site .be /tabindex = - 1>在网站上< / a>< / li> \r\\\

< li>< a target =_ blank\r\ n
href =http://klantenservice.site.be/\r\\\

tabindex = - 1> Klantenservice< / a>< / li> \r \\\

< / ul> \r\\\

< / div> \r\\\

< div class =option-language> \\ \\ r \ n
< ul> \r\\\

< span id =languageForStaticstyle =display:none> nl< / span> \r\ n
<! - IT-4522设计团队使用languageForStatic动态显示文本 - > \r\\\

\r\\\

\r\\ \\ n
< li class =active>< a href =?lang = nltabindex = - 1> NL< / a>< / li> \r\ n
< li>< a href =?lang = frtabindex = - 1> FR< / a>< / li> \r\\\

\ r \\\

\r\\\

\r\\\

< / ul> \r\\\

< / div> \ r \\\

< / div> \r\\\

\r\\\

< div class =branding clearfix> \r\\\

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

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

alt =site>< / div> \r \\\

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

< span id =languageForStaticstyle = none> nl< / span> \ r\\\

<! - IT-5252设计团队使用languageForStatic动态显示文本 - > \r\\\

< / div> \r\\\

< div class =contentContainer> \r\\\

< p> < / p> \r\\\

< / div> \r\\\

< / div> \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' r\\\

\r\\\

\r\\\

< / div> \r\\\

< div class =copyright & copy; site\r\\\

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

- < a href = http://klantenservice.site.be/content/juridische-informatietarget =_ blank\r\\\

tabindex = - 1> Juridische informatie< / a> \r\\ \\ n
- < a href =https://www2.site.be/nl/privacy/\r\\\

href =https://www2.site.be / nl / privacy /target =_ blank\r\\\

tabindex = - 1>隐私< / a>< br> \r\\\

< a href =http://klantenservice.site.be/content/waar-vind-ik-de-algemene-voorwaarden-van-sitetarget =_ blank\r\\\

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

< / div> \r\\\

< / div> \r\\\

< div class =bgContainer>< / div> \r\\\

< / div> \r\\\

\r\\\

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

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

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

\r\\\

& - [if lt IE 9]>!\ r\\\

< script type =text / javascriptsrc =/ openid / js / vendor / jquery.backgroundSize.js> / script>!\r\\\

<![endif] - > \r\\\

\r\\\

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

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

\r\\\

< / body> \r\ n
< / html> \r\\\


我现在被困,我不知道我做错了什么,我不知道下一步做什么。






问题



如何转换我的curl同样的结果?



这是我需要在我的PHP curl设置启用的东西吗?



这是我需要的东西要更新我的Apache设置?



我的PHP CURL有什么问题?



$ p

解决方案

最有可能的问题是,您的第一个请求失败,因为您没有正确设置SSL验证。您必须为这两个请求设置相同的CURL选项。区别只在于URL和POST数据。



因此代码应如下所示:

  $ 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);返回;
}
$ 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);
}


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=benu&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.benunets.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' => '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'

    ) );

  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.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_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

   """
    \r\n
    \r\n
    \r\n
    \r\n
    \r\n
    \r\n
    <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->\r\n
    <!DOCTYPE html>\r\n
    <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->\r\n
    <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->\r\n
    <!--[if gt IE 8]><!-->\r\n
    <html class="no-js">\r\n
    <!--<![endif]-->\r\n
    <head>\r\n
        <title>site: Meld je aan</title>\r\n
        <meta content="IE=edge" http-equiv="X-UA-Compatible">\r\n
        <meta charset="utf-8">\r\n
        <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">\r\n
        <meta name="description" content="">\r\n
    \r\n
        <link rel="shortcut icon" href="//static.site.be/assets/favicon/favicon.ico" type="image/x-icon"/>\r\n
        <link rel="stylesheet" href="css/vendor/normalize.min.css">\r\n
        <link rel="stylesheet" href="css/login.css">\r\n
        <link rel="stylesheet" type="text/css" href="//static.site.be/assets/fonts/omnes-breuer-logo/stylesheet.css">\r\n
        \r\n
        <link rel="stylesheet" href="https://static.uat.site.be/oauth2/css/main.css">\r\n
    \r\n
        <!-- Adobe analytics -->\r\n
        <script src="https://www2.site.be/etc/designs/site-be/js/statistics/SiteCatalyst.js"></script>\r\n
    \r\n
        <script type="text/javascript">\r\n
            function hideHtmlElement(id) {\r\n
                document.getElementById(id).style.display = "none";\r\n
            }\r\n
    \r\n
            function showHtmlElement(id) {\r\n
                document.getElementById(id).style.display = "block";\r\n
            }\r\n
    \r\n
            function showPanel(id) {\r\n
                hideHtmlElement("forgotLogin");\r\n
                hideHtmlElement("showLogin");\r\n
                hideHtmlElement("forgotPassword");\r\n
                showHtmlElement(id);\r\n
            }\r\n
    \r\n
        </script>\r\n
    \r\n
        <!--[if lt IE 9]>\r\n
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>\r\n
        <![endif]-->\r\n
    </head>\r\n
    \r\n
    \r\n
    \r\n
    \r\n
    \r\n
    <div class="wrapper">\r\n
        <span id="showBackgroundForStatic" style="display: none">true</span>\r\n
        <!-- ERROR SCREEN -->\r\n
        <div class="container">\r\n
            <div id="loginContainer">\r\n
                <div id="showLogin">\r\n
                    <div class="options clearfix">\r\n
                        <div class="option-sites">\r\n
                            <ul>\r\n
                                <li><a target="_blank"\r\n
                                       href="http://site.be/nl">Meer site</a></li>\r\n
                                <li><a target="_blank" href="http://site.be/nl"\r\n
                                       tabindex="-1">site.be</a></li>\r\n
                                <li><a target="_blank" href="http://business.site.be/"\r\n
                                       tabindex="-1">Business</a></li>\r\n
                                <li><a target="_blank"\r\n
                                       href="http://corporate.site.be/" tabindex="-1">Over site</a></li>\r\n
                                <li><a target="_blank"\r\n
                                       href="http://klantenservice.site.be/"\r\n
                                       tabindex="-1">Klantenservice</a></li>\r\n
                            </ul>\r\n
                        </div>\r\n
                        <div class="option-language">\r\n
                            <ul>\r\n
                                <span id="languageForStatic" style="display: none">nl</span>\r\n
                                <!-- IT-4522 The design team uses languageForStatic to display text dynamically-->\r\n
                                \r\n
                                    \r\n
                                        <li class="active"><a href="?lang=nl" tabindex="-1">NL</a></li>\r\n
                                        <li><a href="?lang=fr" tabindex="-1">FR</a></li>\r\n
                                    \r\n
                                    \r\n
                                \r\n
                            </ul>\r\n
                        </div>\r\n
                    </div>\r\n
    \r\n
                    <div class="branding clearfix">\r\n
                        <div id="logo"><img src="https://static.site.be/oauth2/images/logosite.png"\r\n
                                            data-at2x="https://static.site.be/oauth2/images/logosite.png@2x.png"\r\n
                                            alt="site"></div>\r\n
                        <h1><label>Fout bij aanmelden</label></h1>\r\n
                        <span id="languageForStatic" style="display: none">nl</span>\r\n
                        <!-- IT-5252 The design team uses languageForStatic to display text dynamically-->\r\n
                    </div>\r\n
                    <div class="contentContainer">\r\n
                        <p>Er ging iets mis. Gelieve de applicatie af te sluiten en daarna opnieuw op te starten.</p>\r\n
                    </div>\r\n
                </div>\r\n
    \r\n
    \r\n
            </div>\r\n
            <div class="copyright">&copy; site\r\n
                <script>document.write(new Date().getFullYear());</script>\r\n
                - <a href="http://klantenservice.site.be/content/juridische-informatie" target="_blank"\r\n
                     tabindex="-1">Juridische informatie</a>\r\n
                - <a href="https://www2.site.be/nl/privacy/"\r\n
                     href="https://www2.site.be/nl/privacy/" target="_blank"\r\n
                     tabindex="-1">Privacy</a><br>\r\n
                <a href="http://klantenservice.site.be/content/waar-vind-ik-de-algemene-voorwaarden-van-site" target="_blank"\r\n
                   tabindex="-1">Tariefinfo en algemene voorwaarden</a>\r\n
            </div>\r\n
        </div>\r\n
        <div class="bgContainer"></div>\r\n
    </div>\r\n
    \r\n
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>\r\n
    <script src="/openid/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>\r\n
    <script src="/openid/js/vendor/retina-1.1.0.min.js"></script>\r\n
    \r\n
    <!--[if lt IE 9]>!\r\n
    <script type="text/javascript" src="/openid/js/vendor/jquery.backgroundSize.js"></script>!\r\n
    <![endif]-->\r\n
    \r\n
    <script src="https://static.uat.site.be/oauth2/js/items.js"></script>\r\n
    <script src="https://static.uat.site.be/oauth2/js/main.js"></script>\r\n
    \r\n
    </body>\r\n
</html>\r\n
"""

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?

解决方案

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

这篇关于将cURL命令翻译成PHP cURL(SSL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆