-1

Hi i have a problem with code below.

I log in to the website using curl. Then I go to the sub and send a POST request to get the results in JSON format. Unfortunately, as a result I get: 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));

I tried to send data in json format, but it unfortunately doesn't help.

EDIT

Piece of data that should display the script.

{"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'>
  • Well maybe the you should first check what return data you’re actually getting from the remote server … – CBroe Jan 26 '14 at 21:52
  • I pasted a sample of the json data. I tried the tips from http://stackoverflow.com/questions/689185/json-decode-returns-null-after-webservice-call but unfortunately still does not work. – user3112825 Jan 26 '14 at 23:19

2 Answers2

0

You are using Content-Type: application/json on request header. So your Post data should be in json format. Use this one for both your POST parameters.

curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($params));
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • Unfortunately does not work. I have tried: curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($params)); curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($params)); curl_setopt($ch,CURLOPT_POSTFIELDS, $params); – user3112825 Jan 27 '14 at 19:34
  • You have used postfields twice. Remove this one `curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($params));` – Sabuj Hassan Jan 27 '14 at 19:37
  • 1. first time use `$ POSTFIELDS = http_build_query ($ fields); ` `curl_setopt ($ ch, CURLOPT_POSTFIELDS $ POSTFIELDS); ` to log on to the page - it works very well. Then I try to download data from the sending request: `$ 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);' ` but it does not work: ( print var_dump (json_decode ($ result, true)); returns NULL – user3112825 Jan 27 '14 at 20:01
  • For second request, use this `curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "GET");` and remove both `post` and `postfields`. I am guessing second one is just a simple `GET` request. – Sabuj Hassan Jan 27 '14 at 20:33
  • I did as you wrote but it still does not work. I also paste the link in an ordinary browser window it returns a 404 error What's more. I downloaded the data, and threw in a json file on my server. Then, using a simple curl script I downloaded them. In this situation, I assume that the problem is with the POST request. – user3112825 Jan 27 '14 at 20:51
0

Problem solved :) !!!! I modified the Request Headers. Valid values​​:

$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";

Many thanks for your help!