Get tweets with Twitter images using PHP,
I want to process only the image URL into an array and return a response with json.

Get the tweet on localhost using the code below,
I was able to return json, but when I uploaded it to the verification server, I could not get the value from Twitter,
An error has occurred.

Error message

Warning: Invalid argument supplied for foreach () in ※※※※※※※※/※※※※※/get_tw_img.php on line 69
source code:
<? php
// Configuration
$bearer_token = "*************************";// Bearer token
$request_url = 'https://api.twitter.com/1.1/search/tweets.json';// Request URL
$search_key = '#hashtag -RT';// search keyword, -RT excludes retweets
// parameters
$params = array (
    'q' =>$search_key,
    'filter' =>'images',
    'count' =>100
// if there are parameters
if ($params) {
    $request_url. = '?'. http_build_query ($params);
// Request context
$context = array (
    'http' =>array (
        'method' =>'GET', // request method
        'header' =>array (// header
            'Authorization: Bearer'. $Bearer_token,
// Request using cURL
$curl = curl_init ();
curl_setopt ($curl, CURLOPT_URL, $request_url);// request URL
curl_setopt ($curl, CURLOPT_HEADER, true);// Get the header
curl_setopt ($curl, CURLOPT_CUSTOMREQUEST, $context ['http'] ['method']);// method
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, false);// do not verify certificate
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);// Returns curl_exec result as a string
curl_setopt ($curl, CURLOPT_HTTPHEADER, $context ['http'] ['header']);// header
curl_setopt ($curl, CURLOPT_TIMEOUT, 5);// timeout in seconds// curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, false);
$res1 = curl_exec ($curl);
$res2 = curl_getinfo ($curl);
curl_close ($curl);
// Retrieved data
$json = substr ($res1, $res2 ['header_size']);// Acquired data (JSON etc.)
$header = substr ($res1, 0, $res2 ['header_size']);// Response header (please use it for verification)

// convert JSON
$obj = json_decode ($json);// convert to object
// $arr = json_decode ($json, true);// convert to array
$_statuses = $obj->statuses;
$img_array = array ();
var_dump ($res1, $res2);
foreach ($_ statuses as $status) {
    $url = $status->entities->media [0]->media_url;
    if (! is_null ($url)) {
        array_push ($img_array, $url);
echo json_encode ($img_array);
Try it

After checking the response value with 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&amp ;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)}

Response was returned and data could not be obtained.

* In localhost, the normal value could be obtained from the API for $res1.

information:(language/FW/tool version etc.)

The PHP versions are as follows.

localhost =>PHP Version 5.5.38
Verification server =>PHP Version 5.2.6

The verification server is connected with[http].
(SSL cannot be used)

Personally, isn't it bad to try to get the value by hitting the API in a non-SSL environment?
What do you think is the cause?

  • Answer # 1

    Can I access the outside from the verification server? It is called.
    Is it ok to try to get yahoo.co.jp html with a simple script?

    I feel like I'm not going out.