Home > Enterprise >  Bad request in PHP
Bad request in PHP

Time:12-19

Hello guys i am trying to run an API request which uses URL based authentication. But whenever the message has some spaces in between the message text i receive the error

failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in C:\wamp64\www\testscripts\wialon.php on line 21

When i take the same url string(with the message having spaces in between) and run it on a browser the message sending is successful bellow is my code

<?php
    $sendname=$_GET['name'];
    $sendage=$_GET['age'];
    $passdat=$_GET['password'];
    $url = 'https://api.smsleopard.com/v1/sms/send?username=O6QDPDmFBDu6BYBTTX3q&password=.$passdat&message='.$sendage.'&destination=254700160125&source=TWIGA';

    $contents = file_get_contents($url);

    //If $contents is not a boolean FALSE value.
    if($contents !== false){
        //Print out the contents.
        echo $contents;
    }
    exit();
}

CodePudding user response:

Php has a built in function for building correctly escaped query strings, I would use that

https://www.php.net/manual/en/function.http-build-query.php

<?php

$sendname = $_GET['name'];
$sendage = $_GET['age'];
$passdat = $_GET['password'];

$url = 'https://api.smsleopard.com/v1/sms/send?'
    . http_build_query([
        'username' => 'O6QDPDmFBDu6BYBTTX3q',
        'password' => $passdat,
        'message' => $sendage,
        'destination' => '254700160125',
        'source' => 'TWIGA',
    ]);

$contents = file_get_contents($url);

//If $contents is not a boolean FALSE value.
if ($contents !== false) {
    //Print out the contents.
    echo $contents;
}
exit();

CodePudding user response:

$url = 'https://api.smsleopard.com/v1/sms/send?username=O6QDPDmFBDu6BYBTTX3q&password='.$passdat.'&message='.$sendage.'&destination=254700160125&source=TWIGA';

you forgot ' multiple times

  • Related