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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!