duanbei3747 2019-04-30 21:29
浏览 145
已采纳

卷曲不打印结果

I've run manually the url from postman, and I've successfully connected and it printed it results.

But when I tried calling it from a method, no result is printing.

here's the curl code I used:

public function curlHandle($apiPath, $postArray)
{
    $CI = & get_instance();
    $conf = $CI->config->config;
    $curlHandle = curl_init($apiPath);
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curlHandle, CURLOPT_USERAGENT,"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36");
    curl_setopt($curlHandle, CURLOPT_TIMEOUT , 300);
    curl_setopt($curlHandle, CURLOPT_POST, 1);
    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postArray);
    curl_setopt($curlHandle, CURLOPT_VERBOSE, 1);
    $fp = fopen('/var/www/html/logs/curl_log.txt', 'w');
    curl_setopt($curlHandle, CURLOPT_STDERR, $fp);
    $result = curl_exec($curlHandle);
    print_r(curl_error($curlHandle));
    if ($result === FALSE) {
        die(curl_error($curlHandle));
    } else {
         $data = json_decode($result, TRUE);
    }
    curl_close($curlHandle);
    return $data;

}

Here's the content of curl_log.txt

  • Hostname localhost/codeignitertest was found in DNS cache
  • Trying 127.0.0.1...
  • TCP_NODELAY set
  • Connected to alocalhost/codeignitertest (127.0.0.1) port 80 (#0)

    POST /test/process HTTP/1.1 Host: localhost/codeignitertest User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36 Accept: / Content-Length: 142 Content-Type: multipart/form-data; boundary=------------------------78a116d61ad4939d

< HTTP/1.1 200 OK < Date: Tue, 30 Apr 2019 21:19:43 GMT < Server: Apache/2.4.29 (Ubuntu) < Cache-Control: no-store, no-cache, must-revalidate < Pragma: no-cache < Set-Cookie: ci_session=ag85kv438n0ukb1rvrkb5cdfkrd8s2q6; expires=Tue, 30-Apr-2019 21:49:43 GMT; Max-Age=1800; path=/; HttpOnly < Expires: Thu, 19 Nov 1981 08:52:00 GMT < Content-Length: 14 < Content-Type: text/html; charset=UTF-8 < * Connection #0 to host localhost/codeignitertest left intact

also note that curlHandle() is from another project and localhost/codeignitertest but both are running in my localhost

  • 写回答

1条回答 默认 最新

  • dongman5539 2019-04-30 21:44
    关注

    You've issued this option:

    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
    

    Which means that curl returns the output from the remote server instead of printing it.

    You then capture that output into a variable:

    $result = curl_exec($curlHandle);
    

    Which you then encode and return to your caller:

    return $data;
    

    This code shouldn't print the results. It returns the results.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计