NodeJS REST API is not accessible from PHP CURL - php

I have a NodeJS API to send push notifications(both GCM and APN) and its working smoothly.
Now I have an Admin Panel built in PHP Codeigniter and I want to call Push API using cURL. Code goes like this
<?php
$url = "http://my_ip:9100/service/api/push";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
?>
But it fails to connect and output is
Failed to connect to my_ip: Permission denied
I've tested Push API from Postman Client Tool and its working smoothly. If I try this in browser at least it'll enters the API method.
Both PHP Project and NodeJS Application are in same server( NGINX ) and I've tried 'localhost' in place of IP Address but its of no use.
What is that I'm missing here ?
Anything that needs to be added while invoking request in cURL( tried many combinations for curl_setopt() )
Any server setup that is blocking the Request from cURL
Thanks in advance.

You haven't allow CORS from your express server. This should only be the reason that refuses the connection from your curl. See this for allowing cors from express server. CORS on ExpressJS

Related

Can't cURL to other server from web server, but can from localhost

I have two servers, one is my webserver, for my website, and the other one is my discord bot hosting server. My setup is when a certain action occurs on the webpage, it'll use cURL to post to the node.js server (the discord bot), which would then return some data.When it was in development and I was creating it on my localhost, it worked perfect (both servers were locally hosted). When I take the servers and run them on their respective services, I am given a connection refused error from cURL, same with file_get_contents().
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://51.161.93.27:8109/');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
echo "<pre>";
var_dump( curl_getinfo($ch) ) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';
curl_close($ch);
If I run this exact code from localhost instead of the webserver, it works and gets the data. On the webserver I can not post or get.
On the webserver, if I remove the port from the CURLOPT_URL, it'll connect to the main ip and return stuff. Same if I put www.google.com. This means cURL works, just can't connect to the port itself.
Note, the specific port is being run with the node.js http module along with express.
The webserver is being run from namecheap, and using cpanel. The discord bot hosting service is called pebblehosting.
Feel free to use the address, http://51.161.93.27:8109/ to find some more information, its the bot address. If able to succesfully get data from it, it should display a json string with a message stating {"status":"fail. You already have a ticket open"}.
I also connected to the address from services like ping.com and was able to connect.
I have spent all day trying to find a solution, but couldn't find anything.

PHP - Retrieve Facebook image in Google Compute Engine

I've tried to retrieve the image data of my Facebook profile picture, using both file_get_contents and curl.
The problem occurs on my Google compute engine instance, while on any other server (localhost - mamp, AWS) the script works fine.
An example for one of the scripts I was using
<?php
var_dump(
json_decode(
file_get_contents("https://graph.facebook.com/_FACEBOOK_ID_/picture?width=720&height=720")
)
);
Please keep in mind that I've tried using the parameter redirect=false, and accessing the image url I've got in my json response returned false as-well.
Also, I've tried using wget in SSH to the image's url, which returned (403) Forbidden.
My assumption is that I need to configure something differently in my server, not PHP, but because I'm able to retrieve any other image, with the same script - I'm not sure what.
I've already experienced this exact problem,
Ignoring SSL verification while using cURL did the trick for me.
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Ignore SSL verification
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/_FACEBOOK_ID_/picture?width=720&height=720");
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);

Google ReCaptcha - Getting Challenge Response on shared server

I have a site that uses the new version of Google ReCaptcha (I am not a robot version), and am having trouble getting the challenge response on my shared server.
I cant use curl or file_get_contents due to restrictions on the server. Is there any other ways to get the response?
The code I was using locally, that does not work on the live site is:
CURL
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$response=$this->get_curl_response("https://www.google.com/recaptcha/api/siteverify?secret=SECRET&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
File Get Contents
$response=$this->get_curl_response("https://www.google.com/recaptcha/api/siteverify?secret=SECRET&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
This turned out to be a problem with the hosting company that could not be resolved, the challenge response from the captcha was being blocked by the hosting company's firewall, and therefore the capthcha always failed.
Because I am on a shared server they could not enable file_get_contents() as it would be a security risk for all the sites on that server.
I installed PHP Captcha (https://www.phpcaptcha.org/) as an alternative, whoch works fine as all the logic is done locally.

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.

getting the POST request from web server

I'm having troubles debugging a POST request I'm making from my web server to another web server.
I'm trying to communicate with a SOAP web service but from some reason a code that worked well from local machine fails when executing on my server
Looking for a way to see the post request my server make to the web service server
web server OS - CentOs
using PHP curl to make the request
Ideas anyone?
Wireshark? If you've got to connect to the remote end using SSL, then run a stunnel client on the soap client and route requests through that tapping in between.
I had the same problem, and using CURLINFO_HEADER_OUT made outgoing request headers show up in debug info.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data = curl_exec($ch);
var_dump($data);
$details = curl_getinfo($ch);
var_dump($details);
To see the POST on the server
$h=fopen('out.txt','w');
fwrite($h,var_export($_POST,true));
Maybe the curl is disabled on your server
Or point your client at:
<?php print_r($_POST); ?>
C.
Redirect the post request to a server you control. You can then read the posted data using echo file_get_contents('php://input');

Categories