简体   繁体   English

如何通过POST请求通过curl从外部网页获取数据

[英]How to obtain data from an external web page with curl through POST requests

I have previously made a code that allows me to make an inquiry to an external web page where I sent by GET the id and it shows me the name and surname of a person. 以前,我已经编写了一个代码,可以查询由GET发送给我的外部网页,该网页显示了一个人的名字和姓氏。 That code works very well, is this: 该代码运行良好,是这样的:

      function nome($id)

        {

        $url = 'https://www.dateas.com/es/consulta_cuit_cuil?name=&cuit='.$id;



        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array("Accept-Lenguage: es-es,es"));
        curl_setopt($ch, CURLOPT_TIMEOUT,10);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $html = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);

         preg_match_all('/<tr class="odd"><td><a href="(.*?)">/',$html ,$matches);

         if (count($matches[1]) == 0) 
         {
            $result = "Error in the server";
         }
         else if(count($matches[1]) == 1)
         {
            $result = $matches[1][0];
            $result = str_replace('/es/persona/','', $result);
            $result = substr($result, 0,-12);
            $result = str_replace('-', ' ', $result);
            $result = ucwords($result);
         }
        return $result; 
            }

    $Name = nome($_GET['id']);
    echo "<br><br>The name of the person is : ". $Name;

This code works well but now I need to extract this information from another website, but to do so I must log in first on that web page and then go to the query section and enter the id, click on the search button and then the page returns the information of the person. 这段代码很好用,但是现在我需要从另一个网站提取此信息,但是要做到这一点,我必须先登录该网页,然后转到查询部分并输入ID,然后单击搜索按钮,然后单击该页面。返回此人的信息。 The website does not make requests by GET but by POST, to search and display the information of a person. 该网站不是通过GET而是通过POST来请求搜索和显示人员信息。

My problem is that I do not know how to extract the information in this new page because I must first login and then look for the person entering their id but the requests are handled by POST. 我的问题是,我不知道如何在此新页面中提取信息,因为我必须先登录然后寻找输入其ID的人,但请求是由POST处理的。

How could I get the information of the person making the query through CURL or any other PHP method? 如何通过CURL或任何其他PHP方法获取进行查询的人员的信息?

this is the page where is the login form to enter the web where the query is made: http://buscardatos.com/Socios/ingresso.php 这是用于登录进行查询的网站的登录表单页面: http : //buscardatos.com/Socios/ingresso.php

After logging in, we should go to: http://www.buscardatos.com/Personas/DNI 登录后,我们应该转到: http : //www.buscardatos.com/Personas/DNI

there is a text field where the id is entered and by clicking on the search button the information of the person is displayed. 在其中输入ID的文本字段中,通过单击搜索按钮,可以显示此人的信息。

the website makes the request by POST. 网站通过POST发出请求。 I have tried to use the same code that I have shown here but it throws me an error of "page not found". 我试图使用与此处显示的代码相同的代码,但是它引发了“找不到页面”错误。

Could you please help me with some code or information about how to solve this issue? 您能否提供一些代码或有关如何解决此问题的信息来帮助我?

Maybe you can try to capture the interaction between your local port and the server with a capture tool (such as Fiddler. In fact, the browser itself can see this information). 也许您可以尝试使用捕获工具(例如Fiddler。实际上是浏览器本身可以看到此信息)来捕获本地端口与服务器之间的交互。

First, you need to use a browser to complete the login and query process, and then you can clearly see the http/https data information that the browser interacts with the server. 首先,您需要使用浏览器来完成登录和查询过程,然后可以清楚地看到浏览器与服务器交互的http / https数据信息。

Next, you need to use PHP code to simulate the browser to submit the login information like the server, and then get the data returned by the server (this data may be a session value, generally stored locally in the browser's cookie.) 接下来,您需要使用PHP代码来模拟浏览器以提交登录信息(例如服务器),然后获取服务器返回的数据(此数据可能是一个会话值,通常存储在浏览器的cookie中。)

Next you can use the CURL above to get the data. 接下来,您可以使用上面的CURL来获取数据。 Remember to bring the data needed for server verification. 请记住携带服务器验证所需的数据。 If it is a cookie, maybe you can add it like this: 如果它是一个cookie,也许您可​​以这样添加它:

curl_setopt($ch,CURLOPT_COOKIE,'cookie= XXXX ; cookie_type= YYYY;');

As long as you provide the correct data and URL, you can get the data you want. 只要提供正确的数据和URL,就可以获取所需的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM