跨域Ajax GET请求与php代理 [英] Cross-Domain Ajax GET request with php proxy

查看:104
本文介绍了跨域Ajax GET请求与php代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我一直在为此扯头发.我正在尝试从公共API获取跨域Ajax GET请求以使其正常工作,但我根本无法使其正常工作.

I have been pulling my hair out over this for the past couple of days. I am trying to get a cross-domain ajax GET request from a public API to work but I just can't get it to work at all.

PHP::我正在使用Ben Alman的ba-simple-proxy( https://github.com/cowboy/php-simple-proxy )

PHP: I am using Ben Alman's ba-simple-proxy (https://github.com/cowboy/php-simple-proxy)

    <?PHP
    $enable_jsonp    = false;
    $enable_native   = false;
    $valid_url_regex = '/.*/';

    $url = $_GET['url'];
    if ( !$url ) {

      // Passed url not specified.
      $contents = 'ERROR: url not specified';
      $status = array( 'http_code' => 'ERROR' );

    } else if ( !preg_match( $valid_url_regex, $url ) ) {

      // Passed url doesn't match $valid_url_regex.
      $contents = 'ERROR: invalid url';
      $status = array( 'http_code' => 'ERROR' );

    } else {
      $ch = curl_init( $url );

      if ( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) {
        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $_POST );
      }

      if ( $_GET['send_cookies'] ) {
        $cookie = array();
        foreach ( $_COOKIE as $key => $value ) {
          $cookie[] = $key . '=' . $value;
        }
        if ( $_GET['send_session'] ) {
          $cookie[] = SID;
        }
        $cookie = implode( '; ', $cookie );

        curl_setopt( $ch, CURLOPT_COOKIE, $cookie );
      }

      curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
      curl_setopt( $ch, CURLOPT_HEADER, true );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

      curl_setopt( $ch, CURLOPT_USERAGENT, $_GET['user_agent'] ? $_GET['user_agent'] : $_SERVER['HTTP_USER_AGENT'] );

      list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', curl_exec( $ch ), 2 );

      $status = curl_getinfo( $ch );

      curl_close( $ch );
    }
    // Split header text into an array.
    $header_text = preg_split( '/[\r\n]+/', $header );
    if ( $_GET['mode'] == 'native' ) {
      if ( !$enable_native ) {
        $contents = 'ERROR: invalid mode';
        $status = array( 'http_code' => 'ERROR' );
      }

      // Propagate headers to response.
      foreach ( $header_text as $header ) {
        if ( preg_match( '/^(?:Content-Type|Content-Language|Set-Cookie):/i', $header ) ) {
          header( $header );
        }
      }

      print $contents;

    } else {

      // $data will be serialized into JSON data.
      $data = array();

      // Propagate all HTTP headers into the JSON data object.
      if ( $_GET['full_headers'] ) {
        $data['headers'] = array();

        foreach ( $header_text as $header ) {
          preg_match( '/^(.+?):\s+(.*)$/', $header, $matches );
          if ( $matches ) {
            $data['headers'][ $matches[1] ] = $matches[2];
          }
        }
      }

      // Propagate all cURL request / response info to the JSON data object.
      if ( $_GET['full_status'] ) {
        $data['status'] = $status;
      } else {
        $data['status'] = array();
        $data['status']['http_code'] = $status['http_code'];
      }

      // Set the JSON data object contents, decoding it from JSON if possible.
      $decoded_json = json_decode( $contents );
      $data['contents'] = $decoded_json ? $decoded_json : $contents;

      // Generate appropriate content-type header.
      $is_xhr = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
      header( 'Content-type: application/' . ( $is_xhr ? 'json' : 'x-javascript' ) );

      // Get JSONP callback.
      $jsonp_callback = $enable_jsonp && isset($_GET['callback']) ? $_GET['callback'] : null;

      // Generate JSON/JSONP string
      $json = json_encode( $data );

      print $jsonp_callback ? "$jsonp_callback($json)" : $json;

    }
    ?>

我试图从中获取信息的API是Aviationweather API.端点网址:

The API I am trying to get info from is the aviationweather API. Endpoint url:

https://www.aviationweather.gov/adds/dataserver_current/httpparam?datasource=metars&requestType=retrieve&format=xml&mostRecentForEachStation=constraint&hoursBeforeNow=1.25

jQuery:

function getMetarToTable(){
        var proxy = 'simple-proxy.php',
        url=proxy + '?' +'https://www.aviationweather.gov/adds/dataserver_current/httpparam?datasource=metars&requestType=retrieve&format=xml&mostRecentForEachStation=constraint&hoursBeforeNow=1.25';

    var stationt="KJFK";
    $.ajax({
    type: "GET",
    url: url,
    crossDomain: true,
    data: {'stationString': stationt},
    datatype: "xml",
    error: function(jqXHR, textStatus, errorThrown) {
    console.log('Error: ' + errorThrown);
     },
    success: function(xml) {
    $("#metarweatherpage").html('');
    $(xml).find('METAR').each(function(){

    var sRawtext = $(this).find('raw_text').text();
    var sStationid = $(this).find('station_id').text();
    $("#metarweatherpage").append('<p><h2>'+sStationid+'</h2></p><p>'+sRawtext+'</p>');

     });
    },

    });

};

我将非常感谢我能提供的任何帮助.

I would really appreciate any help I can get.

推荐答案

来自PHP:

$url = $_GET['url'];

它正在尝试从名为url的查询字符串参数中读取URL.

It is trying to read the URL from a query string parameter called url.

通过JavaScript:

From the JavaScript:

url=proxy + '?' +'https://www.aviationweather.gov/adds/dataserver_current/httpparam?datasource=metars&requestType=retrieve&format=xml&mostRecentForEachStation=constraint&hoursBeforeNow=1.25';

您正在传递原始数据,而没有将其编码为查询字符串.

You are passing the data raw, without encoding it as a query string.

要这样做:

var url = 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?datasource=metars&requestType=retrieve&format=xml&mostRecentForEachStation=constraint&hoursBeforeNow=1.25'
var query = encodeURIComponent("url") + "=" + encodeURIComponent(url);
var proxy_url = proxy + "?" + query;


也就是说,您已经在使用jQuery,所以您不应该首先手动构建URL:


That said, you are using jQuery already so you shouldn't be constructing the URL manually in the first place:

var url = 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?datasource=metars&requestType=retrieve&format=xml&mostRecentForEachStation=constraint&hoursBeforeNow=1.25'

$.ajax({
    type: "GET",
    url: proxy,
    data: {'url': url},
    // etc

这篇关于跨域Ajax GET请求与php代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆