dpv21589
2018-02-04 12:53
浏览 135

在传递变量时从Jquery AJAX执行cURL请求

I've researched a lot about PHP cURL and already know a fair amount of Jquery Ajax however what i'm trying to do isn't straight down the line. I'm making an API call using an API Login button that returns a users nickname.

I then make an ajax request to a php file containing the curl paramaters within which i want to pass the users nickname.

RESOLVED

Using data in the AJAX Request i was able to successfully send the javascript variable nickname as a parameter.

The variable was then accessible via $nickname = $_GET['nickname']; in the cURL PHP file.

Thank you to rollstuhlfahrer for providing the solution marked below.


Ajax Request:

var nickname = API.getUserInfo().value.nickname;
$.ajax({
  method: 'GET',
  url: "/curl-php-file.php",
  cache: false,
  data: {
    nickname: nickname, // Sends data as a URL paramater
  }
  success: function(response) {
    // Data
    var data = response.data.data;
    console.log(data);
  },
  error: function(response, val) {
    console.log("AJAX Request Failed");
  }
});

cURL PHP File:

$nickname = $_GET['nickname']; // Gets data from AJAX
require_once("../../../../../wp-load.php");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://.../?nickname=' . $nickname,
CURLOPT_HTTPHEADER => array(
  'Accept: application/json',
  'Authorization: MYTOKEN'
)
));
$resp   = curl_exec($curl);
wp_send_json_success(json_decode($resp));
curl_close($curl);
exit;

图片转代码服务由CSDN问答提供 功能建议

我研究了很多关于PHP cURL的知识,并且已经知道了相当数量的Jquery Ajax然而我正在尝试 要做的不是直截了当。 我正在使用API​​登录按钮进行API调用,该按钮返回用户昵称。

然后我向包含curl参数的php文件发出ajax请求,我想在其中传递 用户昵称。

已解决

在AJAX请求中使用 data 我能够成功发送javascript变量昵称作为参数。

然后可以通过cURL PHP文件中的 $ nickname = $ _GET ['nickname']; 访问该变量。

感谢 rollstuhlfahrer 提供下面标明的解决方案。


n

Ajax请求:
  var nickname = API.getUserInfo()。value.nickname; 
 $ .ajax({
 method:'GET',\  n url:“/ curl-php-file.php”,
 cache:false,
 data:{
 nickname:nickname,//将数据作为URL参数发送
} 
成功:函数(响应)  {
 // Data 
 var data = response.data.data; 
 console.log(data); 
},
 error:function(response,val){
 console.log(“AJAX Request 失败“); 
} 
}); 
   
 
 

cURL PHP文件:
  $ nickname =  $ _GET [ '昵称'];  //从AJAX获取数据
require_once(“../../../../../ wp-load.php”); 
 $ curl = curl_init(); 
curl_setopt_array($ curl,array(  
 
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL =>'https://.../?nickname ='。$ nickname,
CURLOPT_HTTPHEADER =>数组(
'接受:application / json',
'授权 :MYTOKEN'
)
)); 
 $ resp = curl_exec($ curl); 
 
wp_send_json_success(json_decode($ resp)); 
curl_close($ curl); 
exit; 
   
 

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhang8144 2018-02-04 13:17
    已采纳

    Since you are using GET, you can just pass it along as a query parameter:

    $.ajax({
      method: 'GET',
      url: "/curl-php-file.php?nickname=" + nickname,
      cache: false,
      success: function(response) {
        // Data
        var data = response.data.data;
        console.log(data);
      },
    }
    

    Another option. This has the beauty if built-in character encoding:

    $.ajax({
      url: "/curl-php-file.php",
      method: "get", 
      data: { 
        nickname: nickname,
      },
    

    Source

    Then in your PHP code, you retrieve the passed parameter:

    $nickname = $_GET['nickname'];
    

    And append it to your URL:

    CURLOPT_URL => 'https://.../?nickname=' . $nickname,
    

    You should do some checking, if the nickname is a valid one before sending it off via curl.

    点赞 打赏 评论

相关推荐 更多相似问题