卷毛返回true而不是值 [英] Curl returns true instead of value

查看:89
本文介绍了卷毛返回true而不是值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的API.但是,不知道为什么,但是我得到的是真实值,而不是应该返回的值.

I'm trying to make a simple API. However, not sure why but I'm getting true instead of the value that should be returned.

这是卷曲代码.

try
        {
        $target_url = "my url";
        $post = array(
            'auth_token' => '123456' // post your auth token here
            //'file_contents' => '@' . $_FILES['file']['temp_name']); //the name of the input type is file, you can change it in your HTML.
            );
        $ch = curl_init();
        if($ch == false)
        {
            echo "Couldnt initialize curl";
        }
        curl_setopt($ch, CURLOPT_URL,$target_url);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $result=curl_exec ($ch);
        if (FALSE === $result)
            throw new Exception(curl_error($ch), curl_errno($ch));
        var_dump($result);
        curl_close ($ch);
        }
        catch(Exception $e)
        {
            trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);
        }

获取内容的文件:-

return $_POST['auth_token']

但是, var_dump 的结果是bool(true)而不是123456. 为什么会这样?

However, the result of var_dump is bool(true) instead of 123456. Why is this happening?

推荐答案

您需要echo而不是return:

echo $_POST['auth_token'];

并且您需要CURLOPT_RETURNTRANSFER来使curl_exec()返回内容:

And you need CURLOPT_RETURNTRANSFER to have curl_exec() return the content:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_exec

成功返回TRUE,失败返回FALSE.但是,如果 设置了CURLOPT_RETURNTRANSFER选项,它将在以下位置返回结果 成功,失败则为FALSE.

Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.

这篇关于卷毛返回true而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆