2014-01-26 67 views
-1

嗨,我有一個下面的代碼有問題。獲取JSON結果到一個PHP數組與POST請求

我使用curl登錄到網站。然後我去到sub併發送POST請求以JSON格式獲得結果。不幸的是,結果我得到:NULL

// options 
$EMAIL   = '...'; 
$PASSWORD   = '...'; 
$cookie_file_path = "cookies/cookies.txt"; 
$LOGINURL   = "https://www.domainname.com/auth/login"; 
$agent   = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"; 


$ch = curl_init(); 

$headers[] = "Accept: */*"; 
$headers[] = "Connection: Keep-Alive"; 
$headers[] = "Content-Type: application/json"; 


curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);   
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 



$fields = array(); 
$fields['username'] = $EMAIL; 
$fields['password'] = $PASSWORD; 



$POSTFIELDS = http_build_query($fields); 

curl_setopt($ch, CURLOPT_URL, $LOGINURL); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS); 

$result = curl_exec($ch); 


    $params = array(
    'akcja' =>'test1', 
    'Bank' =>'null', 
    'Produkt' =>'test2', 
    'ProwKOd' =>'undefined', 
    'domainKey'=>'326', 
); 
    $remotePageUrl = 'http://www.domainname.com/users/Links'; 
    curl_setopt($ch,CURLOPT_POST,count($params)); 
    curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($params)); 
    curl_setopt($ch, CURLOPT_URL, $remotePageUrl); 
    $result = curl_exec($ch); 

    curl_close($ch); 



print var_dump(json_decode($result,true)); 

我試圖JSON格式發送數據,但遺憾的是它並不能幫助。

編輯

應該顯示腳本的一段數據。

{"tabela":"\r\n\t\t\t\t<div class='provisionBox'>\r\n\t\t\t\t <img src = '\/img\/promotedIcon.png' class='promotionSign' \/>\r\n\t\t\t\t <div class='provisionBoxLeft'>\r\n\t\t\t\t \t<div class='provBoxRodzajIcon'> <img class='' src='\/_img\/product_icon\/konto_osobiste.png' alt='Konto osobiste' \r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttitle='Konto osobiste'><\/div>\r\n\t\t\t\t\t\t<div class='provBoxProductKind'>Rachunek osobisty \"Konto osobiste PLN\"<\/div>\r\n\t\t\t\t\t\t<div class='clear'><\/div>\r\n\t\t\t\t \t<div class='provBoxBankLogo'> 
+0

嗯,也許你應該首先檢查你實際從遠程服務器獲得的返回數據...... – CBroe

+0

我粘貼了一個json數據樣本。 我嘗試了http://stackoverflow.com/questions/689185/json-decode-returns-null-after-webservice-call的提示,但不幸的是仍然無法正常工作。 – user3112825

回答

0

您是在請求頭使用Content-Type: application/json。所以你的發佈數據應該是json格式。使用這個爲你的兩個POST參數。

curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($params)); 
+0

不幸的是不起作用。我曾嘗試過: curl_setopt($ ch,CURLOPT_POSTFIELDS,json_encode($ params)); curl_setopt($ ch,CURLOPT_POSTFIELDS,http_build_query($ params)); curl_setopt($ ch,CURLOPT_POSTFIELDS,$ params); – user3112825

+0

您已經使用了兩次postfields。刪除這個'curl_setopt($ ch,CURLOPT_POSTFIELDS,http_build_query($ params));' –

+0

1.第一次使用 '$ POSTFIELDS = http_build_query($ fields); ' 'curl_setopt($ ch,CURLOPT_POSTFIELDS $ POSTFIELDS); ' 登錄到頁面 - 它工作得很好。 然後我嘗試從發送請求中下載數據: '$ remotePageUrl ='http://www.domainname.com/users/Links'; ' 'curl_setopt($ ch,CURLOPT_POST,count($ params)); ' 'curl_setopt($ ch,CURLOPT_POSTFIELDS,json_encode($ params)); ' 'curl_setopt($ ch,CURLOPT_URL $ remotePageUrl);' ' ,但它不工作:( 打印的var_dump(json_decode($結果,真)); 返回NULL – user3112825

0

問題解決了:) !!!! 我修改了請求頭。 有效值:

$headers[] = "Accept: application/json, text/javascript, */*; q=0.01"; 
$headers[] = "Connection: Keep-Alive"; 
$headers[] = "Content-Type: application/x-www-form-urlencoded"; 
$headers[] = "X-Requested-With:XMLHttpRequest"; 

非常感謝您的幫助!