2012-07-03 36 views
6

Tôi cần trợ giúp để viết lại mã curl PHP này (sử dụng một tệp * .pem - CA cert, Client cert, khóa riêng trong một tệp):Sử dụng curl trong php với "chứng chỉ CA, Chứng chỉ ứng dụng khách và Khóa cá nhân" trong các tệp riêng biệt

curl_setopt($curl, CURLOPT_URL, $this->url); 
curl_setopt($curl, CURLOPT_HEADER, 0); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($curl, CURLOPT_SSLCERT, $this->keystore); 
curl_setopt($curl, CURLOPT_CAINFO, $this->keystore); 
curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $this->keystorepassword); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $post); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

Vì vậy, nó có thể sử dụng chứng chỉ CA, giấy chứng nhận khách hàngPrivate Key trong các tập tin riêng biệt.

Như trong ví dụ dòng lệnh này:

curl -d "var1=value1&var2=value2&..." -G -v --key key.pem --cacert ca.pem --cert client.pem:xxxxxx https://www.somesite.com/page

Trả lời

15

Dưới đây là một kịch bản PHP với một bản dịch theo nghĩa đen của cuộc gọi dòng lệnh của bạn:

<?php 

    $data = "var1=value1&var2=value2&..."; 
    $url = "https://www.somesite.com/page"; 


    $keyFile = "key.pem"; 
    $caFile = "ca.pem"; 
    $certFile = "client.pem"; 
    $certPass = "xxxxxx"; 

    // Initialise cURL 
    $ch = curl_init($actualUrl); 

    // The -d option is equivalent to CURLOPT_POSTFIELDS. But... 
    // PHP's libcurl interface does not implement the -G flag - instead you would 
    // append $data to $url like this: 
    $actualUrl = $url.'?'.$data; 
    curl_setopt($curl, CURLOPT_URL, $actualUrl); 

    // The -v flag only makes sense at the command line, but it can be enabled 
    // with CURLOPT_VERBOSE - in this case the information will be written to 
    // STDERR, or the file specified by CURLOPT_STDERR. I will ignore this for 
    // now, but if you would like a demonstration let me know. 

    // The --key option - If your key file has a password, you will need to set 
    // this with CURLOPT_SSLKEYPASSWD 
    curl_setopt($ch, CURLOPT_SSLKEY, $keyFile); 

    // The --cacert option 
    curl_setopt($ch, CURLOPT_CAINFO, $caFile); 

    // The --cert option 
    curl_setopt($ch, CURLOPT_SSLCERT, $certFile); 
    curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $certPass); 

    /* 
    Now we should get an identical request to the one created by your command 
    line string, let's have a look at some of the other options you set... 
    */ 

    // CURLOPT_HEADER is disabled by default, there's no need for this unless you 
    // enabled it earlier 
    //curl_setopt($curl, CURLOPT_HEADER, 0); 

    // Your command line string forces a GET request with the -G option, are you 
    // trying to POST or GET? 
    //curl_setopt($curl, CURLOPT_POST, true); 

    // We don't need body data with a GET request 
    //curl_setopt($curl, CURLOPT_POSTFIELDS, $post); 

    // Since we've gone to all the trouble of supplying CS information, we might 
    // as well validate it! 
    //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
+0

Thank you very much Tôi sẽ thử ngay khi về nhà. – somerandomusername

+33

Tôi hy vọng anh ấy về nhà được. – phatfingers

+0

Ahaha! Tôi đã mất vài giờ để tìm ví dụ được chú thích này để hiểu rằng 'CURLOPT_CAINFO' tương đương với cờ' --cacert'. Tôi đã sử dụng một cách ngớ ngẩn bằng cách sử dụng 'CURLOPT_SSLCERT' - dường như rõ ràng. Kudos, @DaveRandom. –

Các vấn đề liên quan