2017-02-19 93 views
1

我一直在試圖每週都去嘗試和使用PHP捲曲能夠連接到我的網絡服務,但是,我不能讓它工作,所以我試圖用捲曲的命令行讓我吃驚..有效。轉換Linux的捲曲PHP

下面是我使用Linux的捲曲使用的命令:

curl -k -i -H "Content-type: application/x-www-form-urlencoded" -c cookies.txt -X POST https://<host>/appserver/j_spring_security_check -d "j_username=admin&j_password=demoserver"

你如何將它轉換爲PHP代碼?

PS。我是新手,剛剛接觸過不到一個月的PHP,請原諒我! :d

+1

[將命令行cURL轉換爲PHP cURL]可能重複(http://stackoverflow.com/questions/1939609/convert-command-line-curl-to-php-curl) – gaurav

回答

1

您提出以下Linux終端curl命令如何與PHP捲曲的選項:

curl -k -i -H "Content-type: application/x-www-form-urlencoded" -c cookies.txt -X POST https://192.168.100.100:444/appserver/j_spring_security_chec‌​k -d "j_username=admin&j_password=demoserver"

這裏是上面的選項/標誌的列表:

  • - K = CURLOPT_SSL_VERIFYPEER:假
  • -i = CURLOPT_HEADER:真
  • -H = CURLOPT_HTTPHEADER
  • -C = CURLOPT_COOKIEJAR + CURLOPT_COOKIEFILE
  • -X POST = CURLOPT_POST:真
  • -d = CURLOPT_POSTFIELDS

這將導致完全以下:

<?php 
    $ch = curl_init(); 
    $url = "https://192.168.100.100:444/appserver/j_spring_security_chec‌​k"; 
    $postData = 'j_username=admin&j_password=demoserver'; 
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_POST, 1); // -X 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postData); // -d 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     'application/x-www-form-urlencoded' 
    )); // -H 
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); // -c 
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); // -c 
    curl_setopt($ch, CURLOPT_HEADER, true); // -i 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // -k 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // see comment 
    echo curl_exec ($ch); 
    curl_close ($ch); 

我希望這幫助你。

+0

感謝@mevdshchee,您提供了噸幫助!我幾乎在那裏..你有什麼想法爲什麼PHP捲曲不會像Linux捲曲一樣通過?這是我執行linux curl時的結果。 'HTTP/1.1 302實測值 日期:星期四,2017年2月16日19點12分22秒GMT 服務器:Apache/2.2.26(UNIX)的mod_ssl/2.2.25的OpenSSL/1.0.1e的mod_jk/1.2.37 套裝 - Cookie:JSESSIONID = 9D1761E49919B0FCE8E077E70E250749;路徑= /應用服務器/;僅Http 地點:https://192.168.100.100:444/appserver/portal/welcome;jsessionid=9D1761E49919B0FCE8E077E70E250749 的Content-Length:0 訪問控制允許來源:* 內容類型:text/plain' –

+0

你可能需要添加一個'CURLOPT_FOLLOWLOCATION'選項來跟蹤重定向。我將這添加到代碼中。 – mevdschee