繁体   English   中英

jQuery $ .ajax()到PHP CURL

[英]jQuery $.ajax() to PHP CURL

我希望将以下内容翻译成PHP CURL。

$.ajax({
  url:"MY_URL",
  cache: false,
  type: "POST",
  data: "",
  dataType: "xml",
  success: function(data) { 
    alert('Eureka!')
    }
});

我特别想知道如何更改以下内容以将dataType设置为xml。 以下代码无效:

$ch = curl_init('MY_URL');
curl_setopt($ch, CURLOPT_HEADER, 0); // get the header 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,array (
      "Content-Type: xml; charset=utf-8",
      "Expect: 100-continue",
      "Accept: xml"
     ));

谢谢!

使用phery,你可以用一种非常直接的方式(在http://phery-php-ajax.net/中 ),在过去的两年里我一直在改进和使用我的库:

// file would be curl.php
Phery::instance()->set(array(
  'curl' => function($data){
    $ch = curl_init($data['url']);
    curl_setopt($ch, CURLOPT_HEADER, 0); // get the header 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
    curl_setopt($ch,CURLOPT_HTTPHEADER,array (
      "Content-Type: xml; charset=utf-8",
      "Expect: 100-continue",
      "Accept: xml"
    ));
    $data = curl_exec($ch);
    curl_close($ch);
    return PheryResponse::factory()->json(array('xml' => $data)); 
  }
))->process();

然后使用ajax调用创建一个函数,使其可重用:

/* config would be {'url':'http://'} */
function curl(url){
  return phery.remote('curl', {'url': url}, {'target': 'curl.php'}, false);
}

curl('http://somesite/data.xml').bind('phery:json', function(event, data){
  // data.xml now contains the XML data you need
}).phery('remote'); // call the remote curl function 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM