get json using php

get json using php

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Fetching JSON from a Third-Party API in PHP

You are attempting to build a server-side proxy that accepts a word parameter, constructs a dynamic URL, and retrieves JSON data from an external source. The code snippet you posted shows the correct logic for building the query string: taking $_GET['vorto'] and appending it between the base path /ajax/epo/eng/ and the callback suffix /?callback=?. However, your attempt to use fopen() and fread() fails because these functions expect a file path or a valid stream resource, not a raw URL string. Passing a URL directly to fopen() does not automatically trigger an HTTP request; it will simply fail or return a resource that cannot be read as expected without further wrapper logic.

To solve this on the server side, you need to use PHP's built-in cURL (Client URLs) library. cURL is the standard tool for making HTTP requests and handles the underlying socket management required to fetch remote data. It parses headers, manages redirects, and provides a clean interface for reading response bodies. Before writing your own script from scratch, check if the target URL supports CORS. If the server at vortaro.us.to sends appropriate Access-Control-Allow-Origin headers, you could technically bypass the backend entirely by calling the API directly from JavaScript using fetch() or $.ajax(). However, for a true server-side implementation where you control the request logic, cURL is the robust choice.

Here is how to refactor your code using cURL. First, initialize the handle and set the necessary options. You must specify that this is an HTTP GET request (CURLOPT_HTTPGET) and ensure that PHP outputs the raw data rather than echoing headers or being affected by redirects unexpectedly.

<?php
$vorto = $_GET['vorto'] ?? '';

if (empty($vorto)) {
    echo "No Vorto provided.";
} else {
    $url1 = "http://vortaro.us.to/ajax/epo/eng/";
    $url2 = "/?callback=?";
    // Ensure the word is URL encoded if it contains special characters
    $word = rawurlencode($vorto); 
    $finalurl = $url1 . $word . $url2;

    $ch = curl_init();
    
    curl_setopt_array($ch, [
        CURLOPT_URL => $finalurl,
        CURLOPT_RETURNTRANSFER => true, // Returns the response as a string instead of outputting it directly
        CURLOPT_FOLLOWLOCATION => true,  // Follows redirects (301/302)
        CURLOPT_SSL_VERIFYPEER => true, // Verifies SSL certificate for HTTPS (good practice even if using HTTP)
        CURLOPT_USERAGENT => 'LaravelCompanyBot' // Identifies the script to avoid blocking by some servers
    ]);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    if ($response === false) {
        echo "cURL error: " . curl_error($ch);
    } else {
        // The URL uses 'callback=?' which implies a JSONP pattern. 
        // If the server returns wrapped data like function(data), you may need to strip it.
        echo $response; 
    }
}
?>

A critical detail in your original example is the callback=? parameter. This indicates the API uses JSONP (JSON with Padding) rather than raw JSON. In a JSONP response, the data is wrapped inside a JavaScript function call, for example: myCallback({"word": "TheWordYouWant"}). Standard JSON parsers like json_decode() will fail on this because it expects {...} at the very start of the string.

If you are using Laravel or a modern framework, you might prefer not to use raw cURL in your controller logic but rather leverage the client-side request if CORS allows it. Alternatively, if you need to parse the JSONP response server-side, you can strip the function name and the parentheses before decoding. Be aware that relying on JSONP is generally considered a legacy pattern due to security risks related to cross-site scripting (XSS), as it executes arbitrary JavaScript on the client side. If the API provider supports Accept: application/json headers or offers a dedicated JSON endpoint without the callback wrapper, switch to that immediately. It simplifies error handling and removes the need for post-processing strings.

Our network