PHP - Need help figuring out why RestAPI wont respond with my Curl call
I have a curl call that I will use to get data from a RestAPI but no mather what I do I end up with a 404 error. When I run the query string directly in the browser it works for some reason even tho I am using the same headers and authentication.
Here you can see the Curl call that is being made:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $query_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$headers = [];
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Cache-Control: no-cache';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
curl_close($ch);
The query URL is being built up by a custom build query
function as seen below. This is mostly to avoid an issue I had with the URL not formating correctly:
function custom_build_query($query_data) {
$query = [];
foreach ($query_data as $name => $value) {
$value = (array) $value;
array_walk_recursive($value, function($value) use (&$query, $name) {
$query[] = urlencode($name) . '=' . urlencode($value);
});
}
return implode("&", $query);
}
Here you can see the whole code:
<?php
function custom_build_query($query_data) {
$query = [];
foreach ($query_data as $name => $value) {
$value = (array) $value;
array_walk_recursive($value, function($value) use (&$query, $name) {
$query[] = urlencode($name) . '=' . urlencode($value);
});
}
return implode("&", $query);
}
$SupplierIdentification = "xxxxxxxxxxxxxxxx";
$username = "xxxxxxxxxxx";
$password = "xxxxxxxxxxx";
$orgnr = $_COOKIE['orgnr'];
$company_name = $_COOKIE['company_name'];
$adress = $_COOKIE['adress'];
$city = $_COOKIE['city'];
$post = $_COOKIE['post'];
$fornavn = $_COOKIE['fornavn'];
$etternavn = $_COOKIE['etternavn'];
$telefon = $_COOKIE['telefon'];
$email = $_COOKIE['email'];
$creditRating = $_COOKIE['creditRating'];
$malepunkt = $_COOKIE['malepunkt_id'];
$data_array = [
'company_info' => [
'orgnr' => $orgnr,
'company_name' => $company_name,
'adress' => $adress,
'city' => $city,
'post' => $post,
'creditRating' => $creditRating,
],
'contact_info' => [
'fornavn' => $fornavn,
'etternavn' => $etternavn,
'telefon' => $telefon,
'email' => $email
],
'MeteringPoints' => $malepunkt
];
$malere = [];
foreach ($malepunkt as $malepunkt){
array_push($malere, $malepunkt);
}
$query_url = "https://esmart24web.no/tk/api/ischange/api/elhubmeteringpoints?";
$query = [
'utilityType' => 'ElectricalPower',
'supplierIdentification' => $SupplierIdentification,
'organizationNumber' => $orgnr,
'meteringPointIdentification' => $malere
];
foreach ($malepunkt as $malepunkt){
$query += ['meteringPointIdentification' => $malepunkt];
}
$query_url .= custom_build_query($query,'','&');
echo $query_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $query_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$headers = [];
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Cache-Control: no-cache';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
curl_close($ch);
print_r($data);
How many English words
do you know?
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
how many words do you know
Powered by Examplum