Search code examples
phpfile-get-contents

How do I get a $something included in an http query


This is from the API og gatewayapi.com:

<?php
// Query args
$query = http_build_query(array(
    'token' => 'my-token',
    'sender' => 'ExampleSMS',
    'message' => 'Hello World',
    'recipients.0.msisdn' => 4512345678,
));
// Send it
$result = file_get_contents('https://gatewayapi.com/rest/mtsms?' . $query);
// Get SMS ids (optional)
print_r(json_decode($result)->ids);

But I would like to include $something to the message part, it fail when I do this:

    <?php

    $something = "my message";

    // Query args
    $query = http_build_query(array(
        'token' => 'my-token',
        'sender' => 'ExampleSMS',
        'message' => '$something',
        'recipients.0.msisdn' => 4512345678,
    ));
    // Send it
    $result = file_get_contents('https://gatewayapi.com/rest/mtsms?' . $query);
    // Get SMS ids (optional)
    print_r(json_decode($result)->ids);

Solution

  • You need to remove ' from variable. Other wise it will not pass variable and take $something as value

        $something = "my message";
    
        // Query args
        $query = http_build_query(array(
            'token' => 'my-token',
            'sender' => 'ExampleSMS',
            'message' => $something,
            'recipients.0.msisdn' => 4512345678,
        ));
        // Send it
        $result = file_get_contents('https://gatewayapi.com/rest/mtsms?' . $query);
        // Get SMS ids (optional)
        print_r(json_decode($result)->ids);
    

    Refer the manual : http://php.net/manual/en/language.types.string.php