Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bobdebower's avatar

request a externe api

Hey guys, i want to be able to get a json data from a externe api and convert it to xml using my api call and my methode in my controller . but i don't know how to tackle this.

( i currently have my route like this)

in my api/route

Route::get('CustomApiUrl', ApiController::class);

Route::post('CustomApiUrl', ApiController::class);

and this is the methode i want to use to convert json to xml from the requested custom url(route).

// i think it looks like this but not sure.

public function index()
   {

 $data= data;

$result = ArrayToXml::convert($data, 'config', true, 'UTF-8');

return response($result)->header('Content-Type', 'text/xml');

}

so i want to be able to get the data from an externe api (customapiRouteURL) and convert the xml using my api call.

0 likes
13 replies
assoft's avatar

routes/api.php

Route::post("get-xml", [ApiController::class, 'getXML']);

ApiController.php


public function getXML(Request $request) 
{
    $result = ArrayToXml::convert($request->my_array, 'config', true, 'UTF-8');

    return response($result)->header('Content-Type', 'text/xml');
}
bobdebower's avatar

Thnakyou for your reply! but where do i put the server adres where i want to get the json from the externe api. so i can convert it to xml.?

here?

   Route::post("is this the place? ", [ApiController::class, 'getXML']);
bobdebower's avatar

thanks! i keep getting the respond in postman as "not found" 404. i don't understand

i did evrything you told me and when i go to the api url. with the http://127.0.0.1:8000/api/: the apilink works. but it doesn't work with my localhost request hmmm. anyway how to dd a route to find out what iam doing wrong?

bobdebower's avatar

okay.

hereis my route in my routes/ api.php

         Route::get('https://test.io/test/test{ID}&content=true', [ApiController::class, 'getXML']);

postman:

post request

i type the 1 to create a endpoint.

with key ID and value 1

returns http://127.0.0.1:8000/api/https://test.io/test/id=1&content=true

and i make the request.

returns bodynot found

here is the api controller

 public function getXML(Request $request)
    {
    $result = ArrayToXml::convert($request->my_array, 'config', true, 'UTF-8');

    return response($result)->header('Content-Type', 'text/xml');
}

and i go to the https://test.io/test/id=1&content=true. (i get back the data. so the link works)

test is ofcourse not the real linkname.

assoft's avatar

Requirements

https://laravel.com/docs/8.x/http-client

composer require guzzlehttp/guzzle

SampleController.php

use Illuminate\Support\Facades\Http;

public function testApiMethod($id) {
    $response = Http::get("https://test.io/test/id={$id}&content=true");
    return response()->json($response->json(), 200);
}

routes/api.php

Route::get('/test/{id}', [SampleController::class, 'testApiMethod']);

Send get request your app: http://127.0.0.1:8000/api/test/1

Another sample:

routes/api.php

Route::get('test/{id}', function ($id) {
    $response = Http::get("https://jsonplaceholder.typicode.com/posts/$id");
    return response()->json($response->json(), 200);
});

request uri: http://127.0.0.1:8000/api/test/10

return

{
    "userId": 1,
    "id": 2,
    "title": "qui est esse",
    "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
}

aaaaaand i'm done :) Good luck buddy.

1 like
bobdebower's avatar

you are aswsome tahnkyou sir! last thing, how can i still use my converter in this examples?

assoft's avatar
    $array = (array)json_decode($response->body());

    $xml = ArrayToXml::convert($array, 'config', true, 'UTF-8');
1 like

Please or to participate in this conversation.