也许我做错了。但我正在 Laravel 中做我的第一个 MVC。这是我的设置。
该应用程序将从各种来源下载数据并保存到数据库以供输出。
我有两个控制器。一种将数据保存到数据库,另一种从 instagram 下载数据。在 Instagram 控制器中。我目前只是输出数据。我想用我的保存控制器来保存它。
Instagram 控制器:
class InstagramController extends BaseController {
public function read($q)
{
$client_id = 'ea7bee895ef34ed08eacad639f515897';
$uri = 'https://api.instagram.com/v1/tags/'.$q.'/media/recent?client_id='.$client_id;
$response = $this->sendRequest($uri);
print_r(json_decode($response));
//return json_decode($response);
}
public function sendRequest($uri){
$curl = curl_init($uri);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$responses = curl_exec($curl);
$responses = json_decode($responses);
curl_close($curl);
echo '<pre>';
foreach ($responses->data as $data) {
//0/{postid}/{url}/{author}/{content}
$type = 0;
$postid = $data->id;
$url = $data->link;
$author = $data->user->username;
$content = $data->images->standard_resolution->url;
echo $type.' '.$postid.' '.$url.' '.$author.' '.$content.'<br />';
}
//var_dump($responses->data);
echo '</pre>';
//return $response;
}
}
保存控制器:
class PostController extends BaseController {
public function save($type, $postid, $url, $author, $content)
{
$post = new Post;
$post->type = $type;
$post->postid = $postid;
$post->url = $url;
$post->author = $author;
$post->content = $content;
try
{
$post->save();
echo $post;
}catch(Exception $e){
throw new Exception( 'Already saved', 0, $e);
}
//
}
}