###前提・実現したいこと
PHPを使ってTwitterの画像付きツイートを取得し、
画像URLのみを配列に加工してjsonでレスポンスを返したいと思っています。
###発生している問題・エラーメッセージ
下記のコードを使って、localhostではツイートを取得し、
jsonを返すことが出来たのですが、検証用のサーバーにアップした時にTwitterから値を取得することが出来ず、
エラーが起こってしまいました。
エラーメッセージ
Warning: Invalid argument supplied for foreach() in ※※※※※※※※/※※※※※/get_tw_img.php on line 69
###該当のソースコード
PHP
1<?php 2// 設定 3$bearer_token = "*************************" ; // ベアラートークン 4$request_url = 'https://api.twitter.com/1.1/search/tweets.json' ; // リクエストURL 5 6$search_key = '#ハッシュタグ -RT'; //検索キーワード, -RTはリツイートを除く 7// パラメータ 8$params = array( 9 'q' => $search_key, 10 'filter' => 'images', 11 'count' => 100 12); 13 14// パラメータがある場合 15if ( $params ) { 16 $request_url .= '?' . http_build_query( $params ) ; 17} 18 19// リクエスト用のコンテキスト 20$context = array( 21 'http' => array( 22 'method' => 'GET' , // リクエストメソッド 23 'header' => array( // ヘッダー 24 'Authorization: Bearer ' . $bearer_token , 25 ) , 26 ) , 27) ; 28 29// cURLを使ってリクエスト 30$curl = curl_init() ; 31curl_setopt( $curl, CURLOPT_URL, $request_url ) ; // リクエストURL 32curl_setopt( $curl, CURLOPT_HEADER, true ) ; // ヘッダーを取得する 33curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $context['http']['method'] ) ; // メソッド 34curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false ) ; // 証明書の検証を行わない 35curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ) ; // curl_execの結果を文字列で返す 36curl_setopt( $curl, CURLOPT_HTTPHEADER, $context['http']['header'] ) ; // ヘッダー 37curl_setopt( $curl, CURLOPT_TIMEOUT, 5 ) ; // タイムアウトの秒数 38 39// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 40// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 41 42$res1 = curl_exec( $curl ) ; 43$res2 = curl_getinfo( $curl ) ; 44curl_close( $curl ) ; 45 46// 取得したデータ 47$json = substr( $res1, $res2['header_size'] ) ; // 取得したデータ(JSONなど) 48$header = substr( $res1, 0, $res2['header_size'] ) ; // レスポンスヘッダー (検証に利用したい場合にどうぞ) 49 50 51// JSONを変換 52$obj = json_decode( $json ) ; // オブジェクトに変換 53// $arr = json_decode( $json, true ) ; // 配列に変換 54 55$_statuses = $obj->statuses; 56$img_array = array(); 57 58var_dump($res1,$res2); 59 60foreach($_statuses as $status){ 61 $url = $status->entities->media[0]->media_url; 62 if(!is_null($url)){ 63 array_push($img_array,$url); 64 } 65} 66echo json_encode($img_array) ; 67 68?> 69 70
###試したこと
var_dump()でレスポンスの値を確認してみたところ、
var_dump($res1); =>bool(false) var_dump($res2); => array(19) { ["url"]=> string(116) "https://api.twitter.com/1.1/search/tweets.json?q=※※※※※※※※※※-RT&filter=images&count=100" ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0.0307) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(0) ["upload_content_length"]=> float(0) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) }
とレスポンスが返ってきており、データが取得できていませんでした。
※localhostでは、$res1にAPIから正常な値が取得できました。
###補足情報(言語/FW/ツール等のバージョンなど)
PHPのバージョンはそれぞれ下記のバージョンです。
localhost => PHP Version 5.5.38
検証用サーバー => PHP Version 5.2.6
検証用サーバーは**【http】**で接続されております。
(SSL使用不可)
個人的には非SSL環境下でAPIを叩いて値を取得しにいこうとしているのがまずいのかな?
と思っているのですが、原因はどういったことが考えられるのでしょうか?

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/17 01:12
2017/03/17 02:34
2017/03/19 09:55