0

我可以使用 basecamp api 文档中描述的 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);
        }
    });
}

但是尽管我的开发服务器返回了 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); 

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

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 回答