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 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路