0

Basecamp API ドキュメントで説明されている curl メソッドを使用して、bash シェルからメッセージを簡単に作成できます。ただし、私のアプリは 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);
        }
    });
}

しかし、私の開発サーバーは HTTP 200 216 応答を返しますが、basecamp はメッセージを作成せず、返されたデータは表示されません。django csrfの問題を回避するためにphpプロキシを使用しています:

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); 

私の困難がどこにあるのかについてのアイデアはありますか?

4

1 に答える 1

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

于 2014-03-16T15:24:51.950 に答える