douda5706 2011-04-22 08:58
浏览 30
已采纳

too long

I came up with this twitter project in flash, and once i was done and tried to put it online i ran into a sandbox error. From what i've read it looks like i just need to setup a php proxy file. Which I get and understand. All the tutorials i've been able to find were for simple urls with no GET data being passed in the URL. For my project the GET data is dynamic so i can't just put a set url in the php proxy and i dont understand php well enough to figure out how to get the get data into the proxy url.. so here's what i know how to edit in Flex: enter image description here

heres a full example api call i would need to make:

http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=brybam&?page=1

and here is a php proxy script that was reccomended online:

<?php

$post_data = $HTTP_RAW_POST_DATA;

$header[] = "Content-type: text/xml";
$header[] = "Content-length: ".strlen($post_data);

$ch = curl_init( $_GET['url'] ); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

if ( strlen($post_data)>0 ){
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}

$response = curl_exec($ch);     
$response_headers = curl_getinfo($ch);     

if (curl_errno($ch)) {
    print curl_error($ch);
} else {
    curl_close($ch);
    header( 'Content-type: ' . $response_headers['content-type']);
    print $response;
}


?>

Alright so getting started I just need to make the php script into a file something like twitter.php and just put it on my domain. Then I'm assuming in the URL box for the http service setup in Flex put and reenter it as something like this:

http://mydomain.com/twitter.php?screen_name=brybam&?page=1

SO what im asking is because my understanding of php is very limited, how exactly would I take the above script and make it capable of being passed

http://mydomain.com/twitter.php?screen_name=brybam&?page=1

with Flex and be able to take different potential arguments?

I think it may be something like

$page = $_GET['page'];
$screen_name = $_GET['screen_name'];

in the php file, but im not sure where i should be placing the variables into to make them part of the URL

im sure this is cake if you know php and it would be awesome if someone could help me out with this, thanks!

EDIT: I tried this, but got an error (the error is posted under what i tried)

<?php
$page = $_GET['page'];
$screen_name = $_GET['screen_name'];
$url = $_GET['url'];
$post_data = $HTTP_RAW_POST_DATA;

$header[] = "Content-type: text/xml";
$header[] = "Content-length: ".strlen($post_data);

$ch = curl_init("'url'?screen_name='$screen_name'&?page='$page'"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

if ( strlen($post_data)>0 ){
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}

$response = curl_exec($ch);     
$response_headers = curl_getinfo($ch);     

if (curl_errno($ch)) {
    print curl_error($ch);
} else {
    curl_close($ch);
    header( 'Content-type: ' . $response_headers['content-type']);
    print $response;
}


?>

error msg:

The response is not a valid XML or a JSON string.

Error on line 1 of document : The element type "meta" must be terminated by the matching end-tag "". Nested exception: The element type "meta" must be terminated by the matching end-tag "".
  • 写回答

1条回答 默认 最新

  • douguachi0056 2011-04-22 16:30
    关注

    This Works:

    <?php
    
    $post_data = $HTTP_RAW_POST_DATA;
    
    $header[] = "Content-type: text/xml";
    $header[] = "Content-length: ".strlen($post_data);
    
    $screen_name = $_GET['screen_name'];
    $page = $_GET['page'];
    
    $url = "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=$screen_name&page=$page";
    
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    
    if ( strlen($post_data)>0 ){
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    }
    
    $response = curl_exec($ch);     
    $response_headers = curl_getinfo($ch);     
    
    if (curl_errno($ch)) {
        print curl_error($ch);
    } else {
        curl_close($ch);
        header( 'Content-type: ' . $response_headers['content-type']);
        print $response;
    }
    ?>
    

    Although, if you are going to be calling more then just that one api call, you may want to consider building the the request string in AS, and just tacking the whole string to the end of the domain.

    IE.

    you would pass something like "1/statuses/user_timeline.xml?screen_name=brybam&?page=1" to a single parameter.

    And your php would look like this...

    $query= $_GET['query'];
    $url = "http://api.twitter.com/$query";
    

    Or even better, create your own service API that uses your own API calls specific to your application. This way you do not need to rebuild the SWF if and when the twitter API changes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊