简单的cURL响应API

i have been developing a URL shortener and would now like to add the API feature in. I have played around with cURL for a while and cannot get it working.

Can firstly anyone help me to give data via GET in the URL to another website, that website will then process that GET data and then can it return a result to the website that is requesting the data.

How can this work? I only need some simple code for me to then add more functions into.

Can someone give me an example in two separate pieces, one for the requesting code and one for the processing code.

Thank you so much! Harry

you can send the URL by encoding it with urlencode() - like:

GET http://yoursite.com/shorten.php?url=encoded_url

in your shorten.php file you will have access to $_GET['url'] variable, which, when you decode it with urldecode() will get you the original url.

You can learn how to use cURL properly with PHP in the PHP cURL documentation. The documentation there is simple to learn and understand, so long as you have a basic understanding of HTTP.

It's up to you how you process responses from cURL. cURL will give you the HTTP response headers and body. If you're making an API for users to request third party sites, it's up to you how you process the responses you get from those sites.

Why do you need cURL for a URL shortener, may I ask? The job of a URL shortener is to redirect users when they come in through links. Requesting third party sites is not part of the job.