如何获得包含在 http 查询中的 $something

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

这是来自 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);

但是我想在消息部分包含 $something,当我这样做时它失败了:

    <?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);

您需要从变量中删除 '。否则它不会传递变量并将 $something 作为值

    $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);

参考手册:http://php.net/manual/en/language.types.string.php