Naterade
Naterade

Reputation: 2685

PHP curl post works but not returning data

Hello I have a curl class that sends a post to a remote server. On that server I get data from a db and echo it out. The data is collected but I the sending curl server gets not data back. Here is my code:

$ch = curl_init();

self::$CURL_OPTS[CURLOPT_URL] = $this->url;
self::$CURL_OPTS[CURLOPT_POST] = true;
self::$CURL_OPTS[CURLOPT_POSTFIELDS] = $this->get_data();

curl_setopt_array($ch, self::$CURL_OPTS);

$result = curl_exec($ch);

curl_close($ch);

echo $result;

Nothing echos though even though it sends the data to the remote server.

This is a curl class so if you need more code for help let me know.

EDIT********************* I have that and its still not working. here is my class.

<?php
  /**
 * install, update, download, api
 */
abstract class Http {
protected $url;
protected $params;
protected $data;
protected $files;

protected $api_client  = 1;
protected $api_key     = 'xxx';


protected static $CURL_OPTS = array(
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_RETURNTRANSFER => 0,
    CURLOPT_TIMEOUT        => 60,
    CURLOPT_POST           => false,
    CURLOPT_HTTPHEADER     => array()
);

public function set_url($url) {
    $this->url = $url;
}

public function get_url() {
    return $this->url;
}

public function set_data($data) {
    $this->data = $data;
}

public function get_data() {
    return $this->data;
}

public function push() {
    $ch = curl_init();

    self::$CURL_OPTS[CURLOPT_URL] = $this->url;
    self::$CURL_OPTS[CURLOPT_POST] = true;
    self::$CURL_OPTS[CURLOPT_POSTFIELDS] = $this->get_data();
    self::$CURL_OPTS[CURLOPT_RETURNTRANSFER] = 1;

    curl_setopt_array($ch, self::$CURL_OPTS);

    $result = curl_exec($ch);

    curl_close($ch);

    return $result;
}

public function pull() {
    $ch = curl_init();

    self::$CURL_OPTS[CURLOPT_URL] = $this->url;
    self::$CURL_OPTS[CURLOPT_RETURNTRANSFER] = 1;

    $result = curl_exec($ch);

    curl_close($ch);

    return $result;
}

abstract protected function check();
abstract protected function install();
abstract protected function update();
abstract protected function download();
abstract protected function api();

}

Upvotes: 0

Views: 1008

Answers (2)

Aleks G
Aleks G

Reputation: 57346

If you want to get the data as the result of curl_exec, you need to set option CURLOPT_RETURNTRANSFER to true.

From the documentation:

CURLOPT_RETURNTRANSFER    TRUE to return the transfer as
                          a string of the return value of
                          curl_exec() instead of outputting
                          it out directly. 

EDIT: Looking at your curl class, you have this code:

protected static $CURL_OPTS = array(
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_RETURNTRANSFER => 0,       // this line ensures that you do NOT get the data back
    CURLOPT_TIMEOUT        => 60,
    CURLOPT_POST           => false,
    CURLOPT_HTTPHEADER     => array()
);

change it to

protected static $CURL_OPTS = array(
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_RETURNTRANSFER => 1,       // this line ensures that you do get the data back
    CURLOPT_TIMEOUT        => 60,
    CURLOPT_POST           => false,
    CURLOPT_HTTPHEADER     => array()
);

Upvotes: 2

Prosto Trader
Prosto Trader

Reputation: 3527

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Upvotes: 2

Related Questions