当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Issue with Reddit API and cURL creating an account

问题描述:

I'm working on an app that allows users to create an account on reddit (and more). I use cURL and PHP to interface with the Reddit API, this is my code:

 function create_account($username, $password, $captcha_iden, $captcha_answer){

$url = "http://reddit.com/api/register";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HEADER, 1);

curl_setopt($ch, CURLOPT_POST, true);

$data = "user=$username&passwd=$password&passwd2=$password&rem=false&reason=redirect&api_type=json&iden=$captcha_iden&captcha=$captcha_answer";

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$curl_scraped_page = curl_exec($ch);

die($curl_scraped_page);

curl_close($ch);

}

However, all I receive from the Reddit servers is this:

HTTP/1.1 302 Moved Temporarily

Server: AkamaiGHost

Content-Length: 0

Location: http://www.reddit.com/api/register

Date: Wed, 31 Jul 2013 20:02:03 GMT

Connection: keep-alive

HTTP/1.1 404 Not Found

Content-Length: 0

Content-Type: text/html; charset=UTF-8

Server: '; DROP TABLE servertypes; --

Vary: Accept-Encoding

Date: Wed, 31 Jul 2013 20:02:04 GMT

Connection: keep-alive

I'm not entirely sure what that HTTP response means and I'm really confused on how to fix this. cURL runs perfectly fine on my server (I have tested it).

Any ideas?

网友答案:

The first part of the response is switching you over from reddit.com to www.reddit.com. I'm assuming the second request is making a GET, which is not accepted for that endpoint (only POST according to the docs) and so it returns the 404. Try updating the original URL to include the www. and see if that works.

分享给朋友:
您可能感兴趣的文章:
随机阅读: