为什么我要从服务器的ajax调用中获得更多数据作为响应?

问题描述 投票:0回答:1

这是我的代码

$url = 'https://'.$_ENV["MAIL_CHIMP_DC"] .'.api.mailchimp.com/3.0/lists/'.$_ENV["MAIL_CHIMP_LIST_ID"].'/members';
    error_log('url-mail_chimp');
    error_log($url);
    $authorization_header=base64_encode('anystring:'.$_ENV['MAIL_CHIMP_API_KEY']);
    error_log($authorization_header);
    $ch=curl_init($url);
    $data_string = $data;
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:Basic '.$authorization_header));
    $result =curl_exec($ch);
    error_log($result);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    error_log($httpcode);
    curl_close($ch);
    echo 'hello';

这全部是在ajax调用中被调用的控制器内部。但是,当我在jquery console.log(data)处理程序上执行success时,我会收到由curl请求发送过来的完整响应,并且还会在该响应后附加“ hello”。我不知道响应如何发送。

php jquery ajax curl codeigniter-3
1个回答
0
投票

您在curl调用中缺少CURLOPT_RETURNTRANSFER选项。没有这个,curl_exec将不会以字符串的形式返回调用的响应,而是将其直接输出。从manualCURLOPT_RETURNTRANSFER

TRUE,以curl_exec()返回值的字符串形式返回传输,而不是直接输出。

因此,添加

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

返回您的代码,输出应正确返回到$result变量中。

© www.soinside.com 2019 - 2024. All rights reserved.