2013-03-01 58 views
4

我在使用cURL特定頁面時遇到問題。cURL帖子無法運行PHP

一個活生生的代碼工作:http://svgen.com/jupiter.php

這裏是我的代碼:

$url = 'https://uspdigital.usp.br/jupiterweb/autenticar'; 

    $data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1'); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, "FileHere"); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, "FileHere"); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP); 
    curl_exec($ch); 
    curl_close($ch); 

雖然我用了相同的URL和POST數據,的file_get_contents工作:

$options = array('http' => array('method' => 'POST','content' => http_build_query($data))); 
    $context = stream_context_create($options); 
    $result = file_get_contents($url, false, $context); 

    var_dump($result); 

有人可以幫助我? 謝謝。

+0

你得到什麼錯誤? – 2013-03-01 06:21:32

+0

你有沒有得到任何回報? – uvinod 2013-03-01 06:21:40

+0

我無法使用cURL進行身份驗證 – 2013-03-01 06:21:51

回答

5

讓您的文章數據:

$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1'); 
$postData = ""; 
foreach($data as $key => $val) { 
    $postData .=$key."=".$val."&"; 
} 
$postData = rtrim($postData, "&"); 

和變化:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 
+0

加載需要很長時間,但工作! – 2013-03-01 06:29:27

+0

這個答案在你的原始代碼中也是一個錯誤。 – 2013-03-01 06:31:49

+0

爲什麼不使用http_build_query()來代替?檢查我的答案。 – Zeeshan 2015-08-09 09:35:35

7

很可能是SSL驗證問題。

添加

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 

此外,如果你使用CURLOPT_PROTOCOLS選項,它應該是HTTPS因爲你發佈到一個安全的網址

curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); // you currently have http 
+0

沒有工作.. – 2013-03-01 06:22:45

+0

@MaurícioGiordano對不起,0代替錯誤,我的錯誤 – 2013-03-01 06:23:09

+0

仍然不工作.. – 2013-03-01 06:24:06

1

你是一個安全的連接上,爲什麼您使用:

curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP); 

使用instea d:

curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); 
+0

它試過並且不工作...注意:file_get_contents使用HTTP而不是HTTPS並且工作... – 2013-03-01 06:25:11

+0

然後保留'HTTPS',這是一個錯誤:-)'curl'和'file_get_contents'的工作方式不一樣。 – 2013-03-01 06:26:19

3

嘗試這些:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 
curl_setopt($ch, CURLOPT_CAINFO, '/etc/pki/tls/cert.pem'); // path is valid for RHEL/CentOS 

這可確保您「捲曲」的資源具有有效的SSL證書。不建議將「CURLOPT_SSL_VERIFYPEER」設置爲false(0)。

5
$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1'); 

應該已經

$data = http_build_query(array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1')); 

它會自動URL編碼的查詢字符串,以及,比手工方法更安全..