0

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();

}

1
  • Have a look at my updated answer - I showed exactly what you need to change Commented May 8, 2014 at 14:58

2 Answers 2

2

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()
);
Sign up to request clarification or add additional context in comments.

2 Comments

Yes but I override that in the push() method. It sets it to one.
@Naterade Check your return status - is it 200? If not, you may not get anything back.
2
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

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.