LoginSignup
3

More than 5 years have passed since last update.

PHPでプロキシ

Posted at

AS3で,InstagramAPIとかにアクセスしようとするとセキュリティサンドボックス侵害だよって言われるのでPHPでプロキシを作って解決。

proxy.php
<?php
    $url = $_POST['url'];
    $count = 0; 
    $postArr = array ();

    foreach ($_POST as $key => $value) {
        if( $key != "url" ){
            $postArr[ $key ] = $value;
            $count++;
        }
    }
    if( $count > 0 ){
        $postdata = http_build_query( $postArr );
        $header = array(  
            "Content-Type: application/x-www-form-urlencoded",  
            "Content-Length: ".strlen($postdata)
        );
        $opts = array ( 
            'http' => array ( 
                'method'  => 'POST', 
                'header' => implode ("\r\n", $header), 
                'content' => $postdata 
            ) 
        );
        $context = stream_context_create ($opts);
        readfile($url, false, $context);
    }else {
        readfile($url);
    }
?>

このphpに飛びたいurlをPOST送信する。
こんな感じ。

loader.as
var urlLoader:URLLoader = new URLLoader();

var urlVariables:URLVariables = new URLVariables ();
urlVariables.url = "http://ほげほげ";

var urlRequest:URLRequest = new URLRequest ( "proxy.php" );
urlRequest.data = urlVariables;
urlRequest.method = URLRequestMethod.POST;

urlLoader.load ( urlRequest );

APIにPOST送信したい変数はurlと一緒にproxy.phpにPOST送信すればOK

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3