4

I have a node server that hosts 3 different apis, then I run separate server for different applications that communicate to the node server. I seem to get undefined/empty string when trying to curl to the node servers. They work fine with posting from a rEST Console or PostMan, which are apps in Google Chrome. But when I try to cURL from a command line or from PHP, it does not work.

I have tried several different things from the cURL call. I am wondering why this won't work. And what can I do on the server end or on my client end to fix it.

Thanks.

EX cURL call:

$host = 'http://api.codelabs.io:2403/products';
$json = '{"name":"Wine type 2"}';

$ch = curl_init($host);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_PORT, 2403);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$data = curl_exec($ch);

2 Answers 2

3

You need to set the HTTPHEADER , also I commented out the port as it was not neccessary...

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($json))
);

The working code..

<?php

$host = 'http://api.codelabs.io:2403/products';
$json = '{"name":"Wine type 2"}';

$ch = curl_init($host);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($ch, CURLOPT_PORT, 2403);   //<----- Commented out !
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(    //<--- Added this code block
        'Content-Type: application/json',
        'Content-Length: ' . strlen($json))
);
$data = curl_exec($ch);
var_dump($data);

OUTPUT :

string 'HTTP/1.1 200 OK Set-Cookie: sid=475e2c66dcb996b2de367f4c626f0d2c3c3a47df4d83ec9fd251560ff1c5e57df1d5d1f6dafb71fd101c5edc065610bd0c5a1bcdb653cdcd6ba23de0677d4864; path=/; httponly Content-Length: 46 Content-Type: application/json Date: Mon, 14 Apr 2014 06:04:05 GMT Connection: keep-alive

{"name":"Wine type 2","id":"0bef0d72a33b2be8"}' (length=342)

Sign up to request clarification or add additional context in comments.

Comments

1

I am trying to connect to a local node script with:

<?php
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($json))
);
$host = '127.0.0.1';
$json = '{"   
deviceToken":"975247bb6a1f7362ac34272888b9ae82b571d51ade36c5a18a7235a72de55079","topic":"com.meditation.iPujaPro","badge":"3","sound":"ping.aiff"}';

$ch = curl_init($host);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_PORT, 3000);   //<----- Commented out !
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(    //<--- Added this code block
    'Content-Type: application/json',
    'Content-Length: ' . strlen($json))
);
$data = curl_exec($ch);
var_dump($data);
?>

but I am receiving: "HTTP/1.1 404 Not Found" What's the matter? If I connect to the server by linx it works seamlessly.

Also if I connect to any other url or port, it works fine, but if I use port 3000 of the listening node js file I get this error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.