duagfgfn1981 2013-03-23 14:58
浏览 56
已采纳

无法从变量中获取数据

I've made a curl request. I put the curl instructions in one class function:

class Curly {
  var $data;

  function GetRequest($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $this->data = curl_exec($ch);
    curl_close($ch);
    //this is what i was missing --> return $this->data;
  }

I put the database query in another class function.

include('class.curly.php');

class mongoSearchForInfoOnEsd {
  public function getEsdsFbInfo($esdID) {
    $mongoApiKey = "xx";
    $requestParams= "xx";
    $url = "xx";

    $fbInfo = (new Curly)->GetRequest($url);
    //this is what i was missing --> return $fbInfo;
  }

In the index.php, an HTTP post from a webhook is coming through, in which parsing through some strings to obtain 2 ids is handled. I then send one of those ids to the mongodb curl request, everything goes good. The correct response comes back, I only know this b/c of the var_dump of the var_dump in the curly class....BUT in the index file I'm struggling to get to get the data out of the var and assign its values to any variable I want.

How can I get the data out? I know its there, but where? I'm so stuck.

# get ytID from http post
#get EsdID from http post
$httpPostData = file_get_contents('php://input');
$postDataDecoded = urldecode($httpPostData);
$ytID = substr($postDataDecoded, strpos($postDataDecoded, "docid=") + strlen("docid="), );
$esdID = substr($postDataDecoded, strpos($postDataDecoded, "m\": \"") + strlen    ("m\": "),;
*$esdData = (new mongoSearchForInfoOnEsd)->getEsdsFbInfo("$esdID");*
$obj = json_decode($esdData, true);
echo $obj;

OK, I've added return and I can see the data, but no operations are working on returned data.

edit ---> put return in both classes, now its fully operational.

  • 写回答

2条回答 默认 最新

  • dounai1986 2013-03-23 15:07
    关注

    As lazyhammer said you need to write the following in the end of your method GetRequest($url)

    return $this->data;
    

    Also, in a class, a function is called a method.

    To be more explicit.

    var_dump doesn't return the data. it's only sending them to the client(your browser) which will display it.

    to return the data computed in your method back to the caller, you need to use the keyword return at the end of your method. When your computer will see return he bring back the data to the caller. it means everything you write after return won't be executed.

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

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面