如何正确格式化basecamp的ajax POST

时间:2014-03-16 15:18:22

标签: php jquery ajax django basecamp

我可以使用basecamp api docs中描述的curl方法轻松地从bash shell创建一条消息。但是,我的应用程序不是用PHP编写的,所以我希望能够通过基本的ajax帖子访问basecamp服务器。不幸的是,我似乎无法将curl语句翻译成ajax帖子。我虽然这就足够了:

function callBasecamp() {
    var parameters = {  
              user:"[my_basecamp_username]",
              pass:"[my_basecamp_password]",
              userAgent: '[my_app] (my_email)',
              contentType: 'application/json; charset=utf-8',
      data: ({ "subject": "This is a Test Message", "content": "This is test content. Please disregard if notified." }),
             };
    var data = JSON.stringify(parameters);
    $.ajax({
        type: "POST",
        data: data,
        dataType: 'json',
    url: "../../../../site_media/proxy.php?url=https://basecamp.com/[account_id#]/api/v1/projects/[project#]/messages.json?" + data,
        traditional: true,
        success: function(data){
            console.log(data);
        }
    });
}

但是虽然我的dev服务器返回HTTP 200 216响应,但basecamp不会创建消息,我也看不到任何返回的数据。我正在使用php代理来规避django csrf问题:

proxy.php

<?php
// File Name: proxy.php
if (!isset($_POST['url'])) die();
$url = urldecode($_POST['url']);
$url = 'https://' . str_replace('https://', '', $url); // Avoid accessing the file system
echo file_get_contents($url); 

关于我的困难可能在哪里的任何想法?

1 个答案:

答案 0 :(得分:0)

代理无法正确转发。 尝试在proxy.php上添加标题:

header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

对于JSON-P:header('Content-Type: application/javascript');

为file_get_contents()调试帮助器

$url1 = 'https://basecamp.com/999999999/api/v1/projects.json';
$url2 = 'https://basecamp.com/2052106/api/v1/projects/1450272/messages.json';

function send($url, $payload) {
  $ctx = stream_context_create(
    array(
      'http'=>array(
        'header'=> "Content-type: application/json\r\n"
                 . "Access-Control-Allow-Origin: *\r\n"
                 . "User-Agent: myApp (app@app.com)\r\n"
                 . "Accept: xml/*, text/*, */*\r\n",
        'method'=> 'GET',
        //'content'=> $payload,
        'ignore_errors' => true
      )
    )
  );

  $result = file_get_contents($url, 0, $ctx);

  var_dump($http_response_header);

  return $result;

}

// The expected return is:
// There's no Basecamp account at this address. ....
echo send($url1, '');

// expected return: {"status":"404","error":"Not Found"}
echo send($url2, '');

有效负载测试请求的用法:

注意:您可以在stream_content选项中将GET切换为POST:

$url = 'https://basecamp.com/2052106/api/v1/projects/1450272/messages.json';

$payload = '{"user":"123",
             "pass":"123",
             "userAgent":"test test@test.com",
             "contentType":"application/json; charset=utf-8",
             "data": {
                "subject":"This is a Test Message",
                "content":"This is test content. Please disregard if notified."}
             }';

echo send($url, $payload);

另一种方法是在PHP中使用cURL:https://stackoverflow.com/a/15395769/1163786