Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - Curl returns true instead of value

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.

Here's the curl code.

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);
        }

The file which gets the content :-

return $_POST['auth_token']

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to echo not return:

echo $_POST['auth_token'];

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

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_exec

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...