Report this

What is the reason for this report?

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

Posted on March 6, 2018

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.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.