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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀