r/PHPhelp • u/aviboy2006 • 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
4
u/Obsidian-One Jun 17 '24
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.
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?
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/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
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