Error on cURL with POST data

前端 未结 2 1789
自闭症患者
自闭症患者 2021-01-26 08:21

I want to extract data from this URL with Php Curl:

https://www.traveloka.com/en/flight/fullsearch?ap=JKTA.DPS&dt=14-10-2017.NA&ps=1.0.0&sc=ECONOMY

相关标签:
2条回答
  • 2021-01-26 08:39

    As per the URL you have given using the below code, you would get nothing rather the redirection link:

    Replace with the link and parameters with the correct api:

    General format of using CURL is given here:

                $ch = curl_init();
    
               $params = urldecode('{"ap":"' . 'JKTA.DPS' . '","dt":"' . '14-10-2017.NA' . '","ps":"' . '1.0.0'. '","sc":"' . 'ECONOMY'. '"}');
    
    
    
                $url = 'https://www.traveloka.com/en/flight/fullsearch?JsonData=' . $params;
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                // curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
    
                curl_setopt($ch, CURLOPT_HEADER, 0);
    
                // Should cURL return or print out the data? (true = return, false = print)
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
                // Timeout in seconds
                curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    
                // Download the given URL, and return output
                $output = curl_exec($ch);
    
                // Close the cURL resource, and free system resources
                curl_close($ch);
    
                if (!empty($output)) {
                    $x = json_decode($output, true);
                            var_dump($x);   
                       }else{
                          return FALSE;
                       }
    
    0 讨论(0)
  • 2021-01-26 08:51

    What you want to do is called Web scrapping, there are plenty of tools that you can use to do that. For now, go for: https://github.com/FriendsOfPHP/Goutte or https://github.com/duzun/hQuery.php.

    0 讨论(0)
提交回复
热议问题