Curl只能在终端中工作,不能在php中工作


Curl only works in terminal not in php

只是一个简单的脚本curl到一个网站,为我的爱好项目获取一些数据。在终端中,它工作得很好,但在php中,它根本不起作用。我想这和饼干有关

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

返回字符串(145)D"�������Ui) j��Ѡ+ 4����߅z����D紨)= qW�+ G��我~��f; v c�6��^ u摩根富林明nG���ǜ����{��T�。9�年代����= m 9 g���GFU��%�[�/�l�r3 | #�7��H��"

使用json_decode返回NULL

根据PHP手册curl_setopt,第二个参数应该是int或常量选项。试试下面的代码:

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

我还更改了您的最终执行以获得curl错误和curl_info,因为您使用的是详细模式,它将有助于定义您的请求的错误。