0

Just a simple script to curl to a website to take some data for my hobby project. In terminal it works perfectly but with php it doesnt work at all. I think it has to do with the cookies

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.pokemongomap.info/includes/mapdata.php"); 

curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, 'fromlat=52.352772543694165&tolat=52.353516320168715&fromlng=6.672205448722025&tolng=6.6761080628386935&fpoke=1&fgym=1');      
curl_setopt($ch,CURLOPT_HTTPHEADER, array(     
                'Pragma: no-cache',
                'Origin: http://www.pokemongomap.info', 
                'Accept-Encoding: gzip, deflate',
                'Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4', 
                'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36', 
                'Content-Type: application/x-www-form-urlencoded; charset=UTF-8', 
                'Accept: application/json, text/javascript, */*; q=0.01', 
                'Cache-Control: no-cache', 
                'Cookie: PHPSESSID=5q2naanh8gj85utl2m96erjfa3; __atssc=reddit%3B1; cookieconsent_dismissed=yes; __atuvc=4%7C33%2C1%7C34; _ga=GA1.2.1355385211.1471162927; latlngzoom=19[##split##]52.35314443349598[##split##]6.674156755780354', 
                'X-Requested-With: XMLHttpRequest', 
                'Connection: keep-alive', 
                'Referer: http://www.pokemongomap.info/'
        ));


curl_setopt($ch, CURLOPT_VERBOSE, true);



$output = curl_exec($ch);
if ($output === FALSE) {
    printf("cUrl error (#%d): %s<br>\n", curl_errno($ch),
           htmlspecialchars(curl_error($ch)));
}

curl_close($ch);    
var_dump($output);

returns string(145) "�]��� D���)j�Ûi�Ѡ+4����߅z����d紨)=qW�+G��Am~��f;c�6v��^�nG�u JF�ǜ ����{��T�.9�s����=m 9G���GFU��%�[�/��r3|l#�7��H�)�"

when using json_decode it returns NULL.

MichaelAngelo
  • 375
  • 2
  • 19

1 Answers1

1

According to PHP manual for curl_setopt, the second parameter should to be a int or a constant option. Try the code below:

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "http://www.pokemongomap.info/includes/mapdata.php");

    curl_setopt($ch,CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_POSTFIELDS, 'fromlat=52.352772543694165&tolat=52.353516320168715&fromlng=6.672205448722025&tolng=6.6761080628386935&fpoke=1&fgym=1');
    curl_setopt($ch,CURLOPT_HTTPHEADER, array(
              'Pragma: no-cache',
              'Origin: http://www.pokemongomap.info',
              'Accept-Encoding: gzip, deflate',
              'Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4',
              'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
              'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
              'Accept: application/json, text/javascript, */*; q=0.01',
              'Cache-Control: no-cache',
              'Cookie: PHPSESSID=5q2naanh8gj85utl2m96erjfa3; __atssc=reddit%3B1; cookieconsent_dismissed=yes; __atuvc=4%7C33%2C1%7C34; _ga=GA1.2.1355385211.1471162927; latlngzoom=19[##split##]52.35314443349598[##split##]6.674156755780354',
              'X-Requested-With: XMLHttpRequest',
              'Connection: keep-alive',
              'Referer: http://www.pokemongomap.info/'
      ));
    curl_setopt($ch,CURLOPT_ENCODING , "");

    curl_setopt($ch, CURLOPT_VERBOSE, true);


    $output = curl_exec($ch);

    if ($output === FALSE) {
        printf("cUrl error (#%d): %s<br>\n", curl_errno($ch),
               htmlspecialchars(curl_error($ch)));
    }

    $info = curl_getinfo($ch);

    var_dump ($info);
    curl_close($ch);
    var_dump(json_decode($output));

I also changed your final execution to get the curl errors and curl_info, since you are using verbose mode, it will help to define what is wrong with your request.

James
  • 1,653
  • 2
  • 31
  • 60
  • it still only returns bool(false) injecting the cookie directly as a string also doesnt help :\ – MichaelAngelo Aug 23 '16 at 19:08
  • Please, see my new edit and edit your question posting the error output. Since you are using the verbose mode, you will see exactly what you are sending, including your cookie – James Aug 23 '16 at 19:14
  • your link was missing my url :) I added it and inject the cookie directly see the new edit for result – MichaelAngelo Aug 23 '16 at 19:18
  • I changed my code again and included the `curl_getinfo($ch);`. Testing it in my environment i got the 200 HTTP response, so curl is working fine. Now we need to discover what is that strange return message :-P. Possibly, this mesage is encoded in some way. – James Aug 23 '16 at 19:49
  • 1
    hmm when checking the content length on the website its also returns 145. so its the same only the encoding is really weird. using the curl in terminal is outputing a json if i add the --compressed and i added the solution from here http://stackoverflow.com/questions/17744112/php-get-compressed-contents-using-curl add this to your answer if someone else searches it curl_setopt($ch,CURLOPT_ENCODING , ""); – MichaelAngelo Aug 23 '16 at 19:53