简体   繁体   English

使用PHP和Curl隐藏响应

[英]Hiding responses using PHP and Curl

I am reusing a curl function from a long time ago that is now acting differently than I remember. 我很久以前重复使用卷曲功能,现在的行为与我记忆的不同。 In this particular case, I'm authenticating a user's Twitter credentials. 在这种特殊情况下,我正在验证用户的Twitter凭据。 Here's the code as it stands now: 这是现在的代码:

$cred = $_POST['twitter_username'].':'.$_POST['twitter_password'];
$url = "http://twitter.com/account/verify_credentials.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_GET, 0);
curl_setopt($ch, CURLOPT_USERPWD, $cred);
$result = curl_exec ($ch);
curl_close ($ch);

This is working fine for the authentication, but is outputting the whole JSON response to the browser, which I don't want to do. 这对于身份验证工作正常,但是将整个JSON响应输出到浏览器,我不想这样做。

I'm not very familiar with curl. 我不太熟悉卷曲。 I tried setting CURLOPT_VERBOSE to 0 and false, but neither worked. 我尝试将CURLOPT_VERBOSE设置为0和false,但都没有工作。 I'm sure this is a pretty simple change somewhere, but I'm lose on what it is. 我确信这是一个非常简单的改变,但我失去了它的本质。

Thanks 谢谢

You need this option: 你需要这个选项:

curl_setopt(CURLOPT_RETURNTRANSFER, true);

From the curl docs : 卷曲文档

CURLOPT_RETURNTRANSFER : TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. CURLOPT_RETURNTRANSFER :TRUE将传输作为curl_exec()返回值的字符串返回,而不是直接输出。

i wrote this function to help simplify my CURL requests 我写了这个函数来帮助简化我的CURL请求

function curl_http_request ($url, $options)
{
    $handle = curl_init($url);
    curl_setopt_array($handle, $options);
    ob_start();
    $buffer = curl_exec($handle);
    ob_end_clean();
    curl_close($handle);
    return $buffer;
} 

example of use 使用的例子

$options = array(
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_USERPWD => $cred
);

curl_http_request($url, $options);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM