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.
Right-click the PHP page you want to use to create the posts. Click "Open With" and select your preferred PHP editor.
Create the API connection using the PHP cURL function. The following code connects to Craigslist:
$cc = new cURL(); $url = 'https://post.craigslist.org/bulk-rss/post';
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)';
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");
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."
Jim Campbell has been a computer engineer for over five years. He excels in hardware repair, computer programming and troubleshooting, and software design. He is currently attending Florida Atlantic University, pursuing a master's degree in computer and electrical engineering and fine-tuning his technical writing abilities.