PHP curl post工作但不返回数据

时间:2014-05-08 14:45:14

标签: php curl response

您好我有一个curl类,它将帖子发送到远程服务器。 在该服务器上,我从数据库获取数据并将其回显。收集数据但是 我发送curl服务器得不到数据。这是我的代码:

$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;

尽管它将数据发送到远程服务器,但没有任何回声。

这是一个curl类,所以如果你需要更多代码来帮助我,请告诉我。

EDIT ********************* 我有它,它仍然无法正常工作。这是我的班级。

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

}

2 个答案:

答案 0 :(得分:2)

如果您希望将数据作为curl_exec的结果,则需要将CURLOPT_RETURNTRANSFER选项设置为true

来自文档:

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

编辑:查看您的curl类,您有以下代码:

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

将其更改为

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

答案 1 :(得分:2)

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);