Home
PHP
Tech Tube
MySQL
Linux
CSS&HTML
JavaScript

Debug PHP HTTP GET requests

This method was created in order to help a client to debug the issues with sending an HTTP GET request via PHP.
<?php

/**
* Use this method to gain debug data for unsuccessful HTTP requests.
* @author Samui Banti - https://samiwell.eu
* @param string $remoteHostURL - The remote URL.
* @return array $testResult - Some sueful data.
*/
function outboundTest($remoteHostURL)
{
    // Set the result array:
    $testResult = array();
    
    // Parse remote host URL:
    $parse = parse_url($remoteHostURL);
    
    // Get the remote host IP:
    $testResult['REMOTE_HOST_IP'] = gethostbyname($parse['host']);
    
    // Get the local IP address:
    $testResult['LOCAL_HOST_IP'] = file_get_contents('http://ipecho.net/plain');
    
    // Attempt to get remote contents using SSL:
    $testResult['FILE_GET_CONTENTS_SSL'] = file_get_contents($remoteHostURL);

    // Attempt to get remote contents using WITHOUT SSL:
    $contextOptions = [
        'ssl' => [
            'verify_peer' => FALSE,
            'verify_peer_name' => FALSE,
        ],
    ];
    $testResult['FILE_GET_CONTENTS_NO_SSL'] = file_get_contents($remoteHostURL, FALSE, stream_context_create($contextOptions));
    
    // Try to get remote contents via CURL:
    $curl = curl_init();
    curl_setopt_array($curl, [
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $remoteHostURL,
    ]);
    $testResult['CURL_SSL'] = curl_exec($curl);
    curl_close($curl);

    // Try to get remote contents via CURL WITHOUT SSL:
    $curl = curl_init();
    curl_setopt_array($curl, [
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $remoteHostURL,
    ]);
    $testResult['CURL_NO_SSL'] = curl_exec($curl);
    curl_close($curl);
    
    // Get the stream wrappers:
    $testResult['STREAM_WRAPPERS'] = stream_get_wrappers();

    return $testResult;
}
Download...