keystonejs / keystone-classic

Node.js CMS and web app framework
http://v4.keystonejs.com
MIT License
14.63k stars 2.21k forks source link

keystone server accepting a PHP cURL #827

Closed jjhesk closed 9 years ago

jjhesk commented 9 years ago

The issue is that when I try make a request from the REST client in DHC on chrome browser it works and the console log down the POST request path with the code 200 immediately. However when I try to make a server side api from php with the same request it will return an error and tell me the "failed to connect" the same path with port 3000. I investigated the cause of the error and I couldn't find anything about it. I found a similar issue on stackover flow on http://stackoverflow.com/questions/23052955/nodejs-server-accepting-a-php-curl

and I did the exactly same thing to my server and didn't work on keystone. I wonder if there are anything or settings that I have not discovered on the api server callback.


class apicall{
    /**
     * @param $url
     * @param array $post
     * @param array $options
     * @return mixed
     * @throws Exception
     */
    protected static function curl_post($url, array $post = NULL, array $options = array())
    {
        $json = json_encode($post);
        $defaults = array(
            // CURLOPT_NOBODY => 1,
            CURLOPT_POST => 1,
            // CURLOPT_HEADER => 0,
            // CURLOPT_SSL_VERIFYPEER => FALSE,
            //CURLOPT_URL => $url,
            // CURLOPT_FRESH_CONNECT => 1,
            CURLOPT_RETURNTRANSFER => 1,
            // CURLOPT_FORBID_REUSE => 1,
            CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13',
            // CURLOPT_VERBOSE => 1,
            // CURLOPT_TIMEOUT => 3,
            CURLOPT_FOLLOWLOCATION => 1,
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $json,
            CURLOPT_HTTPHEADER => array(
                'Content-type: application/json',
                'Content-Length: ' . strlen($json))
        );
        $ch = curl_init($url);
        //  curl_setopt_array($ch, ($options + $defaults));

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
       // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      //  curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
      //  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-type: application/json',
            'Content-Length: ' . strlen($json)));

        $result = curl_exec($ch);

       // var_dump($result);

        if (!$result) {
            // trigger_error(curl_error($ch));
            // self::outFail(19000 + curl_errno($ch), "CURL-curl_post error: " . curl_error($ch));
            //   inno_log_db::log_login_china_server_info(-1, 955, curl_error($ch), "-");

            $message2 = $url;
            $message1 = print_r(($options + $defaults), true);

            // $this->message = $message1;
            throw new Exception("http connection setting: " . curl_error($ch) . "<br/>" .

                $message1

                , 19000);
        } else
            curl_close($ch);

        return $result;
    }
}

apicall::curl_post('http://myhost.com:3000/api/funtion_group1/method/',array(
"domain"=>"item1.com",
"key"=>"xxxxxx"
));

api end point code..


/**
 * Created by hesk on 12/21/2014.
 */
var keystone = require('keystone'),
    async = require('async'),
    _ = require('underscore'),

    utils = require('keystone-utils');

exports = module.exports = function (req, res) {

    var 
        Q = {},
        local = {handle: false};

    /**
     *
     * @param Query
     * @param checkArr
     * @returns {*}
     */
    var input_checker = function (Query, checkArr) {
        _.each(checkArr, function (paramname) {
            if (!Query[paramname]) throw Error(paramname + " is missing.");
        });
        return Query;
    }
    /**
     * async method run business logic
     */
    async.series([
        function (next) {
            try {
                Q = input_checker(req.query, ['domain', 'key']);
                next();
            } catch (e) {
                return next({message: e.message});
            }
        },

        function (next) {

            return res.apiResponse({
                success: true,
                timestamp: new Date().getTime(),

            });
        }
    ], function (err) {
        if (err) {

            console.log('------------------------------------------------------------');

            return res.apiResponse({
                success: false,
                session: false,
                message: (err && err.message ? err.message : false) || 'Sorry, there was an error from verifying your license, please try again.'
            });
        }
    });
};

I did it with hardcoded IP and domain address with http and both did not work.

JedWatson commented 9 years ago

I can't see anything obvious that would be causing the issue, and I have worked on a project where Wordpress successfully connects to a Keystone-powered API to load content.

The fact that the cURL works from Chrome but not PHP (and since I have seen PHP successfully connect to Keystone APIs) makes me suspect it's not a problem with Keystone itself.

There may be something specific to your Keystone config but it's hard to say without seeing all the info (route config, etc)

Our google group would be a better place to ask this question, in case someone else has experience with it or can see the problem you're having.

JedWatson commented 9 years ago

You may also want to post your own question (with the information you've included above) on Stack Overflow, mentioning that the solution to the other question didn't work for you.

Unless you can identify something about Keystone's server config or middleware that's causing the problem (in which case please open another issue with the details) this is out of scope for our issue tracker.

jjhesk commented 9 years ago

I have made the issue on google group and there is the only i can conclude all the conditions: Referred to : https://groups.google.com/forum/#!topic/keystonejs/4x-5HRKeQdc

There are a few scenarios local host php ->request -> this keystone with 3000 port -> not working, and time out fail to connecting to the server code: 28 hosting php ->request -> another keystone framework on 3000 port -> not working, and time out fail to connecting to the server code: 28 hosting php ->request -> another keystone framework on 4100 port -> not working, and time out fail to connecting to the server code: 28 hosting php ->request -> another keystone framework on 4800 port -> not working, and time out fail to connecting to the server code: 28

hosting php ->request -> another nodjs frameowork not keystone -> working browser chrome REST client -> request -> this keystone with 3000 port -> working browser chrome REST client -> request -> this keystone with 4100 port -> working browser chrome REST client -> request -> another keystone framework on 3000 port -> working check aws IP table policy and they are all opened

@JedWatson If you have the working sample from wordpress making requests to keystonejs, please show me the code. Because I have tried many ways to do a simple curl just to get the response from the keystone side.

jjhesk commented 9 years ago

I have no idea but I just figured that the instance on AWS cloud will only allow port 80 for server side request and this is the only port that will allow external php request. I have also checked on the console for custom policies and I found that there was nothing noticed that could block other ports.. maybe someone can help me to clarify this issue.