PHP curl code stops working after a few requests Cent OS 7 server

March 6, 2018 945 views
Apache PHP DigitalOcean CentOS

I created a droplet in Digital Ocean and I have a code in PHP that uses curl to scrape a specific page.

When I created the server and configured it, the code works fine, but after a few requests it stops working. The curl doesn't respond with the correct page, looks like there was a redirect to a wrong page.

I tried to destroy the droplet and start all over again, this time I created a snapshot of the droplet with code working, again after a few requests it stops working, I tried to restore to the snapshot, but it doesn't work. Does anyone know what is happening? I think is some wrong or missing configuration, but I don't figure out what is wrong.

I am using CentOS 7, apache, php 7.2

P.S.: The code works fine in other server that I use in a shared server in Hostgator, and works fine in localhost, too.

2 Answers

Can you share the details of what you are seeing when curl succeeds in pulling the requested document as well as what you are seeing when it fails?

My first thought was that the server you are scraping is using tooling to auto-redirect or restrict clients after a certain number of requests with a Curl user/agent to prevent scraping. This not occurring from other computers makes this more unlikely but not impossible if the site admin decided to only do this for addresses in the IP space of public cloud providers (as Netflix and others do for VPNs). More information would be helpful in helping you figure out the root cause of this.

Hello ryanpq,

Thank you for your reply, I will try to explain what is occurring below.

When I post a request to "http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/valida.asp" with the correct parameters I expect to be redirected to "http://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/Cnpjreva_Comprovante.asp", instead in digital ocean server when I post the request I am redirected to "https://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/cnpjreva_solicitacao2.asp".

Below the class I use and the code I use to instanciate the class.

Class
https://pastebin.com/BarEcXJM

use of class
https://pastebin.com/Lb80TDpZ

you can use the following "CNPJ" to test "45543915000181"

I put a var_dump and an echo on the return of curl in the class, you can try bellow, in the server hosted in digital ocean.

http://www.devbay.com.br/api/testcnpj.php

Have another answer? Share your knowledge.