PHP Fatal error: Uncaught UnirestException: Bad URL
PHP Fatal error: Uncaught UnirestException: Bad URL
I'm trying to request a REST API using PHP Unirest.
The printed error is the following:
Bad URL, colon is first character
And my code:
<?php
require __DIR__ . '/../vendor/autoload.php';
use UnirestRequest as UniRequest;
use CurlFile;
if (isset($_POST['primeiro'])) {
$where = $_POST["where"];
$equal = $_POST["equal"];
$resultado = new pedidos;
$valores = $resultado->LogIgualA($where, $equal);
}
class pedidos {
function LogIgualA($where, $equal) {
$wsURL = "localhost:8080/public";
try {
$valores = UniRequest::get($wsURL."/visual/json_log/where=".$where."/equal=".$equal, $headers, null);
} catch (Exception $e) {
echo $e->getMessage();
}
$valoresAux = $valores->body;
$valores = ;
foreach($valoresAux as $z){
$ID = $z->ID;
$DateConnection = $z->DateConnection;
$TimeToServe = $z->TimeToServe;
$ClientIP = $z->ClientIP;
$CacheCode = $z->CacheCode;
$Bytes = $z->Bytes;
$Method = $z->Method;
$RequestProtocol = $z->RequestProtocol;
$RequestIP = $z->RequestIP;
$RequestPort = $z->RequestPort;
$RequestFolder = $z->RequestFolder;
$Auth = $z->Auth;
$RouteLeft = $z->RouteLeft;
$RouteRight = $z->RouteRight;
$ContentType = $z->ContentType;
}
return $valores;
}
}
The "isset($_POST['primeiro']" is when I click the button in HTML so it calls the function in PHP.
I really don't know how to use this...
localhost:8080/public/public/....
.../json_log/where=something/equal=something
no, i have changed now, but the error presists
– Gabriel Marques
Jun 29 at 9:09
Are you 100% sure that the URL format is correct? That you're not suppose to send in a query string for
where
and equal
? Either way, you should urlencode() those variables before putting them into the URL.– Magnus Eriksson
Jun 29 at 9:11
where
equal
100% I'm not. but I know I have to send the parms in the url. Can I do this?
$valores = $resultado->LogIgualA(urlencode($where), urlencode($equal));
– Gabriel Marques
Jun 29 at 9:17
$valores = $resultado->LogIgualA(urlencode($where), urlencode($equal));
He means:
UniRequest::get($wsURL."/visual/json_log/where=".urlencode($where)."/equ.........
– DanFromGermany
Jun 29 at 12:10
UniRequest::get($wsURL."/visual/json_log/where=".urlencode($where)."/equ.........
1 Answer
1
You need to prepend a protocol like https://
.
https://
There are other people having the same issue where prepending the protocol fixed this... https://github.com/Azure/doAzureParallel/issues/44
URL is not http: localhost:8080/public
localhost:8080/public
Also check that there is no malicious input in your $_POST
vars and maybe you need to use urlencode()
on the fields containing appropriate characters.
$_POST
urlencode()
That would be a pretty awful error message, if that is the issue. Not saying that it isn't the issue, just an observation. :-)
– Magnus Eriksson
Jun 29 at 9:06
the "static" part of the URL is "localhost:8080/public". And if I add the http:// it loads infinitely
– Gabriel Marques
Jun 29 at 9:07
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Where/when do you get that error message? Btw, is the URL really
localhost:8080/public/public/....
? and then.../json_log/where=something/equal=something
? That looks pretty strange.– Magnus Eriksson
Jun 29 at 9:04