用于 flash twitter api 调用的 php 代理(带有动态获取数组)
我在 Flash 中提出了这个 Twitter 项目,当我完成并尝试将其放到网上时,我遇到了沙箱错误。从我读到的内容看来我只需要设置一个 php 代理文件。我得到并理解了。我能找到的所有教程都是针对简单的 url,没有在 URL 中传递 GET 数据。对于我的项目来说,GET 数据是动态的,所以我不能只在 php 代理中放置一个设置的 url,而且我对 php 的理解不够好,无法弄清楚如何将获取数据放入代理 url 中。 这就是我知道如何在 Flex 中编辑的内容:
这是我需要进行的完整 API 调用示例:
http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=brybam&?page=1
这里是推荐的 php 代理脚本在线:
<?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;
}
?>
好吧,开始我只需要将 php 脚本制作成一个类似 twitter.php 的文件,然后将其放在我的域中。然后我假设在 Flex 中的 http 服务设置的 URL 框中输入并重新输入如下内容:
http://mydomain.com/twitter.php?screen_name=brybam&?page=1
所以我问的是因为我对 php 的理解非常有限,我将如何准确地采用上面的脚本并制作它能够通过 Flex 传递
http://mydomain.com/twitter.php?screen_name=brybam&?page=1
并能够接受不同的潜在参数?
我认为这可能类似于
$page = $_GET['page'];
$screen_name = $_GET['screen_name'];
php 文件中的内容,但我不确定应该将变量放入哪里以使它们成为 URL 的一部分,
我确信如果您了解 php,这就是蛋糕,如果有人可以帮助我,那就太棒了解决这个问题,谢谢!
编辑: 我尝试了这个,但收到错误(该错误发布在我尝试过的内容下)
<?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;
}
?>
错误消息:
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 "".
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:
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 "".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有效:
尽管如此,如果您要拨打更多电话,那么只需对于一个 api 调用,您可能需要考虑在 AS 中构建请求字符串,并将整个字符串附加到域的末尾。
IE。
您可以将“1/statuses/user_timeline.xml?screen_name=brybam&?page=1”之类的内容传递给单个参数。
您的 php 看起来像这样...
或者更好的是,创建您自己的服务 API,该 API 使用您自己的特定于您的应用程序的 API 调用。这样,当 twitter API 发生更改时,您无需重建 SWF。
This Works:
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...
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.