简体   繁体   English

使用CURL发布到文件中…不接受变量

[英]Posting to a file using CURL… Not taking variables

Posting to a file using Curl 使用Curl发布到文件

I'm trying to post to a file as soon as user enters a website assuming they have clicked from an ad. 假设用户点击了广告,我就会在用户进入网站后立即将其发布到文件中。

Example url = http://myFabSite.com/?tr=213 示例网址= http://myFabSite.com/?tr=213

This is what I'm trying but its not capturing the tr URL variable or the referrer: 这是我正在尝试的方法,但未捕获tr URL变量或引荐来源网址:

if($_GET['tr']){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://myFabSite.com/actions/tracksAds.php");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true);

    $data = array(
        'referrer' => $_SERVER['HTTP_REFERER'],
        'track_code' => $_GET['tr']
    );

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);    
}

trackAds.php: trackAds.php:

$mysqli = dbConnect();

$referrer = $mysqli->real_escape_string(urldecode(trim($_REQUEST['referrer'])));
$track_code = $mysqli->real_escape_string($_REQUEST['track_code']);

$query = "insert into ad_tracking ( tracking_code, referrer ) VALUES ( '$track_code', '$referrer' )"; 

$result = $mysqli->query($query);

Anything obvious? 有什么明显的吗?

UPDATE 更新

This is from print_r($data); 这是来自print_r($data);

Array
(
[referrer] => none
[track_code] => fb1
)

This is $query from trackAds.php 这是来自trackAds.php的$ query

insert into ad_tracking ( tracking_code, referrer ) VALUES ( '', '' )

So, the array is not being passed, either at all or correctly, to trackAds.php 因此,阵列根本没有或正确地传递到trackAds.php

You're missing quotes in $_GET[tr] on the first line. 您在第一行的$_GET[tr]缺少引号。 Change it to $_GET['tr'] . 将其更改为$_GET['tr']

Also, use $_POST instead of $_REQUEST in trackAds.php . 另外,在trackAds.php使用$_POST代替$_REQUEST There's a great chance here that you are inadvertently getting a cookie value instead of a POST value, or simply have the wrong data in $_REQUEST . 这里很有可能您无意中得到了一个cookie值而不是一个POST值,或者只是在$_REQUEST有错误的数据。 See this page in the documentation . 请参阅文档中的此页面

Actually found a different method which works ok, still no clue why the other doesn't. 实际上找到了一种行之有效的不同方法,仍然不知道为什么其他方法不起作用。

This assumes PHP 5+ 假设PHP 5+

    $url = 'http://myFabSite.com/actions/tracksAds.php';
    $data = array('referrer' => $_SERVER['HTTP_REFERER'], 'track_code' => $_GET['tr']);

    // use key 'http' even if you send the request to https://...
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data),
        ),
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

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

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