Getting curl response from CLI but access denied from PHP - php

For many requests I am getting access denied errors when using curl from php, my code is:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_URL, 'https://www.vueling.com/en');
curl_setopt($ch, CURLOPT_TIMEOUT, '2');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
which returns this:
<H1>Access Denied</H1>
However if I run it directly on the command line:
curl -v https://www.vueling.com/en
I get the full response, my ip is not blocked, im not sure why CLI works but PHP doesnt? Anyone have any ideas?

The CLI version of curl probably sends an user agent that is accepted by this website, but the PHP version of curl may send an user agent that is rejected.
So you have to check that the headers sent by curl are similar in both ways.

Related

CURL not executing or returning an error

I have a dedicated server hosted at OVH and I have the following PHP CURL script that calls the Facebook Graph API to post on a user wall:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v2.6/'.$user.'/feed');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'link='.$link.'&access_token=************');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if(curl_errno($ch)){
error_log(curl_error($ch));
}
else {
error_log($response);
}
The expected behavior is that the script should log the facebook response. I noticed a LOT of times, the script doesn't return ANY response at all and not even a CURL error.
I have also a script that uses file_get_contents. I noticed also that every few days I suddenly get a huge amount of connection timed out calling the Facebook Graph API. It happens randomly and for no reason!
When I try to call the same URL from browser immediately after it's logged in the error log, it works successfully.
I'm really confused :( Do you think these are connectivity issues with OVH servers or the Graph API or what?
Thanks
wanted to comment, not post an answer but I don't have enough rep :(
For https using curl I usually add:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

php curl request over ssl fails abruptly

I'm running a curl request from a php script, very straight forward
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://url-here');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
This works fine over http but as soon as i use https code execution doesn't pass the curl_exec command and i can't see what the error is. The client side i just receive a 'Connection Reset'.
It's worth noting it does the same thing if i use https://google.com.
Also worth noting that running this from the command like works fine so seems to be an issue running from a php script.
I've added the usual CURLOPT_SSL_VERIFYPEER to false and still no joy.
The same code does however work from my local machine so seems to be isolated to the server.
It's an amazon ec2 instance if that makes any difference.
Welcome any ideas.
Many thanks
Figured this out, curl wasn't compiled with SSL. Did this and all began to work.

PHP curl works on localhost but not on the web server?

The below php code is making a REST API call that returns json object. This works fine when i use it on my localhost using WAMP Absolutely no problem.
However when i push this app on the server it would TimeOut and display 503 Service Unavailable.
I checked the logs it has an entry :
The TimeOut Specified has expired.
I contacted my admin he just said this app listens to PORT=64665 and HOST=0.0.0.0. What does that mean? What more changes do i need to make in my code to make it work on the server ? Help
<?php
$url = "http://xyz.net/v2/plan/"; // I have changed the REST URI API Link for security reasons
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, false);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
// Will dump json
var_dump(json_decode($result, true));
$response = "http://xyz.net/v2/plan/";
echo $response;
?>
Check your server's /etc/hosts file, there could be the problem, ie. domain mapped to another (wrong) IP address.

cURL request using socks5 proxy fails when using PHP, but it works through the command line

cURL + proxy noob here, having a hard time. I'm having trouble trying to retrieve a web page from a remote secure server via a proxy. Everything has apparently been set up correctly by a remote dev, such that the following command line instruction works and returns what we're looking for:
curl -k --socks5-hostname localhost:xxxx https://hostname/
However, the following PHP does not echo the requested webpage. Instead it echoes the error 'Couldn't resolve host name':
$proxy = 'localhost:xxxx';
$url = 'https://hostname/';
//$proxyauth = 'user:password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
$curl_scraped_page = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error)
echo $error;
elseif ($curl_scraped_page)
echo $curl_scraped_page;
If the $url is changed to a public page, such as Google, the request is successful and everyone is happy.
The connection requires an SSH tunnel if that changes anything at all. The tunnel is open and functioning, as proven by the command line request succeeding.
Is there something obvious that is being missed here?
You need to set option CURLOPT_PROXYTYPE to CURLPROXY_SOCKS5_HOSTNAME, which sadly wasn't defined in old PHP versions, circa pre-5.6; if you have earlier in but you can explicitly use its value, which is equal to 7:
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
In the option CURLOPT_PROXYTYPE you need to set CURLPROXY_SOCKS5_HOSTNAME option instead of CURLPROXY_SOCKS5.
In this case, the DNS query (for hostname resolving) will be sent to SOCKS proxy and not resolved in the local network.
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
This constant available since PHP 5.5.23 and PHP 5.6.7 and cURL 7.18.0, so you can simply use it.

Using CURL, gives 500 Internal Server Error

I am using CURL and I am getting a 500 internal server error. I am not using the user agent option, could this be causing the issue?
This is the snippet
$current_url="http://localhost/mysite/entercode.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $current_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'variable1=abc&variable2=123');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
The same thing just happened to me.
A 500 Internal Server error when i tried to execute $ch = curl_init();. It turns out php curl wasn't installed on my server. I installed it and presto my code worked!
Check this out for how to install PHP CURL
entercode.php is throwing an error related to your input. I suggest you check the server error log. Your curl implementation is fine.

Categories