I am new to Symfony2 and I’m trying to send a
new Request()
to and external API. This is what I have but I don’t know if it is correct use of the built in request/response library.
$request = new Request('https://myservice.com/apimethod?foo=bar', 'GET');
Can anyone tell me if this will return a response provided the API I’m trying to call exists?! If not, what am I doing wrong?
In Symfony2, the Request class represents an HTTP request made to your site. Basically, if you go to
www.yoursite.com/someactionSymfony instantiates aSymfony\Component\HttpFoundation\Requestobject. This object contains methods that you can use to examine the HTTP request (such as seeing if it contains GET or POST variables.)This is a good explanation of Symfony and HTTP fundamentals. I also recommend looking at the source code for Request to see exactly what it can do.
In order to achieve what you’re trying to do in your example, you’d need to use cURL. I personally use a wrapper class on top of cURL that you can find here.
Hope this helps.