Friday, October 28, 2022

[SOLVED] HTML script (type/javascript) to cURL PHP

Issue

I'm trying to post this script with PHP via cURL;

<script async=1 type=text/javascript src=//trackcmp.net/convert?actid=10000&[email protected]&c=1&v=100&r=&u=https://www.website.com/></script>

I've tried the following cURL;

$conversion_value = isset($args['conversion_value']) ? $args['conversion_value'] : "";
        
if(empty($conversion_value)){
    return false;
}

$window_location_href = "www.website.com";
$document_referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "";

$trackcmp_email = '[email protected]';
$trackcmp_conversion = '1';
$trackcmp_conversion_value = $conversion_value;
$trackcmp = [];
$trackcmp['src'] = 'https://trackcmp.net/convert';
$trackcmp['params'] = array('actid' => '10000', 'e' => urlencode($trackcmp_email), 'c' => $trackcmp_conversion, 'v' => $trackcmp_conversion_value, 'r' => $document_referrer, 'u' => $window_location_href);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $trackcmp['src']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $trackcmp['params']);

$headers = array();
$headers[] = 'Content-Type: text/javascript';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);

$info = curl_getinfo($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

Did I forgot something cURL wise or is it just not going to work?

I would like for these values to be posted to said URL via some sort of webhook eventually.

EDIT: Apparently I've been over-thinking the problem. The reply I've marked as 'answer' solved this problem for me.


Solution

This is what your request looks like to the trackcmp.net server

Content-Length: 618
Content-Type: text/javascript; boundary=------------------------9923620be574b58c
Accept: */*
BODY=
--------------------------9923620be574b58c
Content-Disposition: attachment; name="actid"
10000
--------------------------9923620be574b58c
Content-Disposition: attachment; name="e"
info%40email.com
--------------------------9923620be574b58c
Content-Disposition: attachment; name="c"
1
--------------------------9923620be574b58c
Content-Disposition: attachment; name="v"
--------------------------9923620be574b58c
Content-Disposition: attachment; name="r"
--------------------------9923620be574b58c
Content-Disposition: attachment; name="u"
--------------------------9923620be574b58c--

Remove the post fields.
Add curl_setopt($ch, CURLOPT_HTTPGET, true);
Use this URL:

https://trackcmp.net/convert?actid=10000&[email protected]&c=1&v=100&r=&u=https://www.website.com

And the received GET data will look like it should:

$_SERVER['QUERY_STRING']
actid=10000&[email protected]&c=1&v=100&r=&u=https://www.website.com

So here is your curl code:

$headers = array();
$headers[] = 'Content-Type: text/javascript';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, 'https://trackcmp.net/convert?actid=10000&[email protected]&c=1&v=100&r=&u=https://www.website.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, true);
$response = curl_exec($ch);


Answered By - Misunderstood
Answer Checked By - Senaida (WPSolving Volunteer)