【问题标题】:How to set jquery variable in PHP CURL如何在 PHP CURL 中设置 jquery 变量
【发布时间】:2013-03-09 08:55:25
【问题描述】:

我在 PHP 中有一个这样的屏幕抓取代码:

<?
$url = 'https://www.google.nl/search?q=cars';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
?>

我还有一个 Jquery(ajax) 脚本可以像这样获取代理:

$.get('ThePhpFile.php', function(data){

     $(data).appendTo('div')

}

一切正常,但现在我想将要抓取的 URL 设置为变量,该变量实际上是父文档中某些输入的值(我正在使用 iframe)。我知道如何在 Jquery 中做到这一点:

var TheUrl = $("input", parent.document.body).val();

所以我的问题是如何设置变量以使用 PHP 代码。有必要把它放在PHP代码中吗?我怎么做?

【问题讨论】:

  • 你需要将变量ajax到php
  • @mplungjan 我已经尝试了几件事,但没有运气(不擅长 PHP)如果我谷歌我只会遇到与 PHP 变量方向相反的代码----> Jquery

标签: php jquery ajax variables get


【解决方案1】:

你需要将变量ajax到php:

这会将 ?url=..... 发送到 php

完整示例DEMO

<html>
<head>
<title>Search Proxy</title>
<script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>
<script>
$(function() {
  $("#search").on("submit",function(e){
    e.preventDefault();
    $.get('searchproxy.php', 
     {url:$("#url").val()}, 
       function(data){
         $(data).html('#result');
     });
  });
});
</script>
</head>
<body>
<div>
<form id="search">
<input id="url" type="text" value="" />
</form>
<div id="result"></div>
</body>
</html>

<?php
$url = $url = $_GET["url"]; // you need to add input cleaning
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
?>

【讨论】:

  • 当我将它放入 JsFiddle jsfiddle.net/4ZBEf 时出现“函数中缺少名称”错误
  • 另外你的小提琴没有 html 并且没有运行 jQuery
  • 但是我如何将它与 PHP 代码联系起来。如果我删除 URL 部分($url = 'google.nl/search?q=cars';) 它不起作用..
  • 我的意思是这样的$url = TheUrl();
  • 它对我不起作用..问题可能出在parent.document.body).val() 上,我非常感谢您对这个纬度位的帮助..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-19
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
  • 2015-02-06
相关资源
最近更新 更多