dpwdldgn43486 2017-02-08 18:18
浏览 20
已采纳

php脚本返回值,尽管既没有“echo”也没有“print”,也没有使用“printf”?

I've created this php script a while ago that is working well.

UPDATE

(Removed a lot of text that turns out not relevant for this problem.)

Only thing: I don't know why it is working. (returns $result and I don't know how)

UPDATE

Due to request here the original (sensible info replaced) script in question:

<?php

    $http_origin = $_SERVER['HTTP_ORIGIN'];

    if ($http_origin == "https://some.domains.tld" ||
        $http_origin == "https://some.domains.tld" ||
        $http_origin == "https://some.domains.tld" ||
        $http_origin == "https://some.domains.tld" ||
        $http_origin == "https://some.domains.tld")
    {
      header("Access-Control-Allow-Origin: ".$http_origin);
    }

    ### global variables
    $curl; $mode = $_POST['mode'];

    ### init logging
    $file = fopen('error.txt', 'w');

    ### log request source
    #fwrite($file, "request from: ".$http_origin."
");

    ### create variable parameters
    $api_key='mysecretkey';

    ###Init curl
    #fwrite($file, "Ok, try to include curl..."."
");

    try{
        $curl = curl_init();
    }catch(Exception $e){
        fwrite($file, "curl init failed. Look: "."
".$e."
");
        return null;
    }

    switch ($mode) {
        case 'alldatas':
            alldatas();
            break;

        case 'adata':
            adata();
            break;

        case 'register':
            register();
            break;

        default:
            return;
    }

    ###Get list of all datas
    function alldatas()
    {
        global $curl;
        global $api_key;
        global $file;
        $result;

        $params = ['api_key' => $api_key];

        try{
            curl_setopt($curl, CURLOPT_URL, "https://some.third-party.service/api/v2/ever/webinars");
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

            $result = curl_exec($curl);

        }catch(Exception $e){
            fwrite($file, "data list failed. Look: "."
".$e."
");
            return null;
        }

        return $result;
    }

    ###Get a data
    function adata()
    {
        global $curl;
        global $api_key;
        global $file;
        $result;

        $data_id = $_POST['data_id'];

        if($data_id){

            $params = [
                'api_key' => $api_key,
                'data_id' => $data_id
            ];

            try{
                curl_setopt($curl, CURLOPT_URL, "https://some.third-party.service/api/v2/ever/webinar");
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

                $result = curl_exec($curl);

            }catch(Exception $e){
                fwrite($file, "webinar fetch failed, man. Look: "."
".$e."
");
                return null;
            }

            return $result;

        }

    }

    ###Register user to a datas
    function register(){

        global $curl;
        global $api_key;
        global $file;
        $result;

        $data_id = $_POST['data_id'];
        $name = $_POST['name'];
        $email = $_POST['email'];
        $schedule = $_POST['schedule'];
        $timezone = $_POST['timezone'];

        if($data_id && $name && $email && $schedule && $timezone){

            $params = [
                'api_key'=>$api_key,
                'data_id'=>$data_id,
                'name'=>$name,
                'email'=>$email,
                'schedule'=>$schedule,
                'timezone'=>$timezone
            ];

            try{
                curl_setopt($curl, CURLOPT_URL, "https://some.third-party.service/api/v2/ever/register");
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

                $result = curl_exec($curl);

            }catch(Exception $e){
                fwrite($file, "data list failed. Look: "."
".$e."
");
                return null;
            }

        }
        return $result;
    }

?>
  • 写回答

3条回答 默认 最新

  • douxian3170 2017-02-08 18:57
    关注

    You are not setting CURLOPT_RETURNTRANSFER in your curl_setopt(). What that means is that when curl_exec() is called, the result of the cURL transfer is echoed out! That's where your returned data is coming from.

    When CURLOPT_RETURNTRANSFER is TRUE, then curl_exec() returns the data and echos nothing. But, when CURLOPT_RETURNTRANSFER is FALSE (or unset), then curl_exec() echos the data and returns TRUE.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度