如何获得脸书信使机器人的flickr随机图像


How to get flickr random images for facebook messenger bot?

如果用户要求,我不希望我的机器人接收图像。
但我真的不知道该怎么做
我在我的网站上创建了一个特殊的页面,在那里我可以根据请求获取图片
但是,我的机器人不起作用
我创建了flickr帐户,创建了特殊页面,将我的机器人代码与flickr页面代码连接起来
所以,问题是,如何让我的机器人在flickr中搜索图片,然后将它们提供给用户

以下是机器人程序页面的代码:

 include 'flickr.php';
 $row = "My access token";
 $url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$row;

 $ch = curl_init($url);
 if($message == "image'.$set.'")
{
    $load_images = $hasil;
    $image = file_get_contents($load_images);
    $jsonData = '{
        "recipient":{
        "id":"'.$sender.'"
        },
          "message":{
              "attachment":{
                   "type":"image",
                   "payload":{
                       "url":"'.$image.'"
                  }
             }
        }
    }'; 
 };
 $json_enc = $jsonData;
 curl_setopt($ch, CURLOPT_POST, 1);   
 curl_setopt($ch, CURLOPT_POSTFIELDS, $json_enc);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));  
 if(!empty($input['entry'][0]['messaging'][0]['message'])){
    $result = curl_exec($ch);
 }

这是我的flickr代码
我想错误就在这里,但我看不出来。

 require_once 'webhook.php';
 class flickr
 {
   var $api;
     function __construct($api) {
        $this->api = $api;
      }
  function flickr_photos_search($search,$count_image,$size)
    {
    $params = array(
    'api_key'   => $this->api,
    'method'    => 'flickr.photos.search',
    'text'      => $search,
    'format'    => 'rest',
    'per_page'  => $count_image,
    'page'      => 1,);
    $xml = $this->create_url($params);
    if(@$rsp = simplexml_load_file($xml))
    {
        if (count($rsp)<>0)
        {
            foreach($rsp->photos->children() as $photo)
            {
                if ($photo->getName()=='photo')
                {
                    $farm=$photo->attributes()->farm;
                    $server=$photo->attributes()->server;
                    $id=$photo->attributes()->id;
                    $secret=$photo->attributes()->secret;
                    if ($size=='Med')
                    {
                        $sz="";
                    }
                    else
                    {
                        $sz = "_".$size;
                    }
                    $gbr[]='<img src="https://farm'.$farm.'.staticflickr.com/'.$server.'/'.$id.'_'.$secret.$sz.'.jpg'.'" /> ';
                }
            }
        }
        else
        {
            die("No images found!");
        }
    }else
    {
        die("wrong parameter");
    }
    return $gbr;
  }
 function create_url($params)
    {
    $encoded_params = array();
    foreach ($params as $k => $v){
        $encoded_params[] = urlencode($k).'='.urlencode($v);
    }
    $url = "https://api.flickr.com/services/rest/?".implode('&', $encoded_params);
    return $url;
}
 }
 if(isset($_REQUEST[$set]))
 {
 $search=$_REQUEST[$set];
 $result= 30;
 $size = 'm';
 $flickr = new flickr('My flickr secret code');
 $gbr    = $flickr->flickr_photos_search($search,$result,$size);
 foreach($gbr as $hasil)
    {
        echo $hasil.' ';
    }
 }

出现问题:

$load_images = $hasil;
$image = file_get_contents($load_images);
"payload":{
    "url":"'.$image.'"
}

来自文档-有效载荷图像只是该图像的URL

你可以使用我的API(https://github.com/Fritak/messenger-platform)因此,非常容易使用:

// Send an image (file).
$bot->sendImage($userToSendMessage, 'http://placehold.it/150x150');