SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why use Ternary Operator in http_build_query

    I am reading the book, PHP MASTER: WRITE CUTTING-EDGE CODE. While I am reading Chapter 3 APIs, I got a question about one line of code on page 112.

    The code is on how to make Cross-domain Requests through a proxy script.

    Approximately at the middle of the code, one line is as follows.
    PHP Code:
    $url .= '?' .http_build_query($_GET +($allowed_hosts[$requested_host]['args']) ?: array()); 
    I am curious why the Ternary Operator is used here, why the expression for the TRUE condition is empty, and what the purpose of using an empty array() for the FALSE condition. I would highly appreciate any help on these questions.

    The entire code of the proxy script is copied below.

    PHP Code:
    // An array of allowed hosts with their HTTP protocol (i.e. httpor https) and returned mimetype
    $allowed_hosts = array(
    'api.bit.ly' => array(
    "protocol" => "http",
    "mimetype" => "application/json",
    "args" => array(
    APIs 111
    "login" => "user",
    "apiKey" => "secret",
    )
    )
    );
    // Check if the requested host is allowed, PATH_INFO starts with a /
    $requested_host parse_url("http:/" .$_SERVER['PATH_INFO'],PHP_URL_HOST);
    if (!isset(
    $allowed_hosts[$requested_host])) {
    // Send a 403 Forbidden HTTP status code and exit
    header("Status: 403 Forbidden");
    exit;
    }
    // Create the final URL
    $url $allowed_hosts[$requested_host]['protocol'] . ':/' .$_SERVER['PATH_INFO'];
    if (!empty(
    $_SERVER['QUERY_STRING'])) {
    // Construct the GET args from those passed in and the default
    $url .= '?' .http_build_query($_GET + ($allowed_hosts[$requested_host]['args']) ?: array());
    }
    // Instantiate curl
    $curl curl_init($url);
    //Check if request is a POST, and attach the POST data
    if ($_SERVER['REQUEST_METHOD'] == "POST") {
    $data http_build_query($_POST);
    curl_setopt ($curlCURLOPT_POSTtrue);
    curl_setopt ($curlCURLOPT_POSTFIELDS$data);
    }
    // Don't return HTTP headers. Do return the contents of the call
    curl_setopt($curlCURLOPT_HEADERfalse);
    curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
    // Make the call
    $response curl_exec($curl);
    // Relay unsuccessful responses
    $status curl_getinfo($curlCURLINFO_HTTP_CODE);
    if (
    $status >= "400") {
    header("Status: 500 Internal Server Error");
    }
    // Set the Content-Type appropriately
    header("Content-Type: " .$allowed_hosts[$requested_host]['mimetype']);
    // Output the response
    echo $response;
    // Shutdown curl
    curl_close($curl); 

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,402
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Basically, by leaving the second expression empty in the ternary statement, the result of the first expression will be used if it evaluates to TRUE. I wasn't aware you could do that but, according to the manual, it's been possible since PHP 5.3.

    The reason for passing an empty array on FALSE is that the http_build_query function expects an array as the first argument, so if there aren't any params to pass in, and empty array is passed instead.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •