php sending via Curl nodejs

Asked

Viewed 251 times

-1

Good evening, I’m 2 days trying to send Curl php data to nodejs when I do out the Curl works... but I needed it to go this way.... attempt 1 php:

    {
       $this->ch = curl_init();
     curl_setopt($this->ch,CURLOPT_URL, $url);


  curl_setopt($this->ch,CURLOPT_POST, 1);
  curl_setopt($this->ch,CURLOPT_POSTFIELDS, $data);
  curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($this->ch, CURLOPT_POST, true);

  $result = curl_exec($this->ch);
 return curl_close($this->ch);
 }

attempt 2:

    $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL, $url);
     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, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(    //<--- Added this code block
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data))
);
var_dump($data);
$data = curl_exec($ch)|

in nodejs the post method in no way brings any kind of feedback that I’ve tried a little of everything and I’m missing ideas of how to do or what I’m doing wrong kkk wanted to send via post an xml to the nodejs treat but not even a basic send is going so I feel frustrated ... 4 days in trouble and I’m barely crawling on it ...

  app.post('/teste', function (req, res,next) {

//   console.log(util.inspect(req));
//console.log(util.inspect(req, false, null, true /* enable colors */));;
console.log(req.params);
console.log(req.query);

  console.log('receiving data ...');
    console.log('body is ',req.body);
    res.send(req.body);


});

This method I can get the items and the returns are ok:

app.get('/agencia2', function (req, res,next) {

//  console.log(req.params);
//  console.log(req.body);
  console.log(req.query);
  console.log(req.query.id);

   res.send("quase teste"); 
});
  • Why does it have to be at Curl? It is curious whether it works or has a specific reason?

  • how would you start the process of sending php information ( codeinginter) to nodejs? if you have any suggestions??

  • found in a stack post my question. https://stackoverflow.com/questions/10048978/sending-messages-from-php-to-node-js

1 answer

1


in php I can send everything I need and in nodejs I process the return xml that was not processing in php I am presenting the link that I found the answer to my questions as well as the corresponding code.

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var bodyParser = require('body-parser')
app.use(bodyParser.json());

app.post('/phpcallback', function(req, res) {
    var content = req.body;
    console.log('message received from php: ' + content.msg);
    //to-do: forward the message to the connected nodes.
    res.end('ok');
});

http.listen(8080, function(){
  var addr = http.address();
  console.log('app listening on ' + addr.address + ':' + addr.port);
});

Curl in php:

<?php

$data = array("name" => "Robot", "msg" => "Hi guys, I'm a PHP bot !");                                                                    
$data_string = json_encode($data);

$ch = curl_init('http://localhost:8080/phpcallback');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

echo curl_exec($ch)."\n";
curl_close($ch);

?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.