How to Post to Craigslist API

By Jim Campbell

Craigslist includes an application programming interface, or API, that lets you upload bulk items to the site, so you do not need to post dozens of entries manually. This setup is typically used by real estate people who need to post housing lists at once. You connect to the API using the PHP language, and use the API's functions to post to the server.

Step 1

Right-click the PHP page you want to use to create the posts. Click "Open With" and select your preferred PHP editor.

Step 2

Create the API connection using the PHP cURL function. The following code connects to Craigslist:

$cc = new cURL(); $url = '';

Step 3

Set up the headers to pass to the Craigslist connection. The following code sets up the header information:

$this->headers[] = 'Connection: Keep-Alive'; $this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8'; $this->user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)';

Step 4

Send the data to Craigslist. In this example, the bulk items are stored in a file named "myfile.xml." Change the file name to your own. The following code sends the data:

curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers); curl_setopt($process, CURLOPT_HEADER, 1); curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent); curl_setopt($process, CURLOPT_TIMEOUT, 30); curl_setopt($process, CURLOPT_POSTFIELDS, "myfile.xml");

Step 5

Return the processing response to a PHP variable and print out the results to the browser:

$return = curl_exec($process); $info = curl_getinfo($process); echo $return."