r/PHPhelp • • Jun 17 '24

curl request is throwing 403 in PHP CURL

CURL request in php is throwing 403. This is working fine with ping command, Command line CURL request, working in browser and postman. I tried to pull same container locally it works there but it doesn't work in AWS ECS task. Inside AWS ECS task when I tried to run same URL with CLI CURL its work.

What will be problem ? if it was network issue then it should not have work from CLI CURL. Only happening with PHP CURL code.

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://gissvr.leepa.org/gissvr/rest/services/ParcelsWFS/MapServer',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET'));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

I tried hitting URL In browser and then copy as CURL from network tab. Then imported to Postman then converted to PHP CURL in postman. Used same code. Same PHP code is working locally in same docker image container but not working in ECS task container using same Docker image.

Now one more thing I got to know from official website of leepa.org who provide this URL. is

Working : https://gissvr4.leepa.org/gissvr/rest/services/ParcelsWFS/MapServer

Not working : https://gissvr.leepa.org/gissvr/rest/services/ParcelsWFS/MapServer

ping gissvr.leepa.org

PING e242177.dscb.akamaiedge.net (23.213.203.8) 56(84) bytes of data.

64 bytes from a23-213-203-8.deploy.static.akamaitechnologies.com (23.213.203.8): icmp_seq=1 ttl=41 time=10.4 ms

64 bytes from a23-213-203-8.deploy.static.akamaitechnologies.com (23.213.203.8): icmp_seq=2 ttl=41 time=10.4 ms

2 Upvotes

13 comments sorted by

4

u/Obsidian-One Jun 17 '24

Same PHP code is working locally in same docker image container but not working in ECS task container using same Docker image

So, it's not the code. The code is fine, as you already figured out. I don't know anything about ECS task containers, but a little bit of reading up on Akamai shows this is a relatively common thing for it to thrown 403 errors. Akamai uses anti-bot technology, and it might be detecting the ECS instance as a bot (perhaps due to IP reputation). gissvr4.leepa.org may not be on Akamai servers, so that might be why it's not exhibiting the same issue. The IP address returned is very different, but I can't get a resolved host name on it, so I don't really know. Based on this, I would say this probably isn't a PHP or curl issue, rather an ECS/Akamai service issue. There may be a way around it, but you'd have to research that further.

1

u/aviboy2006 Jun 18 '24

Thanks for showing some lights on different perspective for this issue. I will check with AWS support.

1

u/aviboy2006 Jun 18 '24

Do you have any reference link which I can read about Akamai ?

1

u/Obsidian-One Jun 18 '24

I did some searching yesterday, and came across a number of places that described what might be happening. I know enough about 403 errors to know that sometimes it's a you problem and other times it's a them problem. In other words, sometimes you're doing something that causes it, and other times, they are (such as a misconfiguration of some sort). In this case, I think it's a you problem, or more specifically, something related to Amazon ECS.

I don't recall where I saw it, maybe a Stack Overflow question, but someone was getting a 403 error when calling Amazon APIs from ECS, and one of the commenters said they had to disable private DNS names. I don't know what that means. Maybe you do. Since Akamai uses a special technology to determine the closest web server (incidentally also called ECS, but not the same as Amazon ECS, same acronym, different meaning) perhaps DNS has something to do with this issue.

https://community.akamai.com/customers/s/article/What-is-ECS-EDNS0-Client-Subnet?language=en_US

I wish I knew more to assist you, but I don't. Maybe post this over on the /r/aws subreddit. Perhaps someone with more experience with ECS can help.

3

u/cooper-man Jun 17 '24

A 403 response is a permissions based issue.

Do you have credentials set in any of your examples, and are you definitely using the direct verb (GET, POST, PUT), etc?

Can you export the working php curl request from Postman, to compare to what you're using?

1

u/aviboy2006 Jun 17 '24

This URL is accessible via with out username and password. It is accesible directly from browser. Added CURL PHP request in question.

5

u/Alone_Leading_4685 15d ago

your ping gives it away, the gissvr leepa host is behind akamai, and the reason php curl fails while CLI curl works on the very same ECS box is almost certainly the User-Agent: your php code doesn't set one, so it goes out with an empty/default UA, while command-line curl sends 'curl/xy'. akamai blocks the no-UA request. set a real browser user-agent (CURLOPT_USERAGENT, or add it via CURLOPT_HTTPHEADER) and that alone will likely fix it. as for why it worked locally but 403s from ECS: from your laptop's residential ip akamai is lenient, from aws datacenter ranges it's much stricter, so the same weak request that slipped through at home gets blocked from ecs. two practical notes: you found the gissvr4 leepa host works, so if it serves the same service just use that host. and more importantly this is a public arcgis rest MapServer, so you don't really need to scrape it, hit the query api directly, eg .../ParcelsWFS/MapServer/0/query?where=1=1&outFields=*&f=json, that's the intended interface and returns clean json. if akamai still blocks you from ecs even with a proper UA (datacenter-ip reputation), you'd route through a residential ip, a proxy or a scraping api like scrapfly that handles akamai, but i'd bet the UA fix plus the arcgis query endpoint gets you there.

1

u/minn0w Jun 17 '24

Show your source please, ping as well :-)

1

u/aviboy2006 Jun 17 '24

simple PHP CURL request code. Added in question

1

u/minn0w Jun 17 '24

That cookie isn't sensitive is it? Will it give everyone on the internet access to something they shouldn't?

1

u/aviboy2006 Jun 18 '24

Just simply from browser call. This URL is publicly available. anyway I removed that cookie part.

1

u/PeteZahad Jun 19 '24

I guess that the remote service does not allow the IP address of the client or the user agent (curl/{version}).

Try

curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);

and use a current value for $userAgent, e.g. Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

1

u/aviboy2006 Jun 21 '24

I tried this but didn't work