Page 1 of 1

Why str_replace Not Working Properly With cURL?

Posted: Sat Jun 17, 2017 6:07 pm
by UniqueIdeaMan
Php Buddies,

Why do you reckon the following script is unable to replace the 'https://' or the 'http://' words with 'http://mymydomain.com' ?
It is able to replace the words 'www.' with 'http://mymydomain.com', though.

Open 2 tabs in your browser where one opens to the page where you are running the following script in your wamp/xampp and the other tab should open direct to http://ebay.com for your experiment.

Running the script in your wamp/xampp and hovering your mouse over "fashion" link on ebay would show you 'http//mymydomain.com/tracker.php?ebay.com/global/fashion' and that is proof 'www.' got replaced.
You may confirm this cross checking both tabs by hovering your mouse over the mentioned links.
Now, in the (non xampp/wamp) tab, hover your mouse over the links "register", "sign in", etc. and you will see they start with 'https://'. Then finally, in the other tab (xampp/wamp), hover your mouse over these same links and you'd see the 'https://' have not been replaced with 'http://mymydomain.com'.
Why is that ?
Why is the str_replace failing on these 2 occasions to replace the 'http://' and the 'https://' to 'http://mymydomain.com' ?

Here is the code related to post 22 and so kindly answer my question in post 22 after reading it.
Also, don't forget to answer post 21 first.

Thank you everyone who attempts!

Code: Select all


<?php
$url = "http://www.ebay.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);

/* pattern can be either 2:
$pattern = '#http://#';
$pattern = '/http:\/\//';
*/

$pattern = '/https:\/\//';
$replacement = 'http://mydomain.com/tracker.php?';

$pattern = '/http:\/\//';
$replacement = 'http://mydomain.com/tracker.php?';

$pattern = 'www.';
$replacement = 'http://mydomain.com/tracker.php?';

echo str_replace($pattern, $replacement, $result);

?>


Re: Why str_replace Not Working Properly With cURL?

Posted: Sun Jun 18, 2017 11:45 am
by Pazuzu156
What exactly are you trying to ask? If you would provide code, that might help. Also, clearly state the issue, cause what you're saying doesn't make much sense.

If you're having issues figuring out how str_replace works, look here

Code: Select all

<?php
$url = 'http://ebay.com';
$newurl = str_replace('http://', 'https://', $url);
echo $url.'<br>'.$newurl;

Re: Why str_replace Not Working Properly With cURL?

Posted: Sun Jun 18, 2017 5:14 pm
by UniqueIdeaMan
Pazuzu156,

Sorry man! I forgot to add my code in my original post. Edited it and added it now!

What I am trying to do is get cURL to pull-up the page: http://www.ebay.com.
Then, I'm trying to get the script to replace all "http://" and "https://" and "www." with: http://mymydomain.com/tracker.php?

Code: Select all

<?php
$url = "http://www.ebay.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);

/* pattern can be either 2:
$pattern = '#http://#';
$pattern = '/http:\/\//';
*/

$pattern = '/https:\/\//';
$replacement = 'http://mymydomain.com/tracker.php?';

$pattern = '/http:\/\//';
$replacement = 'http://mymydomain.com/tracker.php?';

$pattern = 'www.';
$replacement = 'http://mymydomain.com/tracker.php?';

echo str_replace($pattern, $replacement, $result);

?>
For some reason the str_replace is getting overwritten on each cycle.
Therefore, tried with the following code but still no luck !

Code: Select all

<?php
$url = "http://www.ebay.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);

$pattern = 'https://';
$replacement = 'http://mydomain.com/tracker.php?';

$pattern = 'http://';
$replacement = 'http://mydomain.com/tracker.php?';

$pattern = 'www.';
$replacement = 'http://mydomain.com/tracker.php?';

echo str_replace($pattern, $replacement, $result);

?>

Re: Why str_replace Not Working Properly With cURL?

Posted: Sun Jun 18, 2017 8:31 pm
by Pazuzu156
Okay, well I decided to not use str_replace in favor of using preg_replace_callback using a super simple regular expression, which searches for http:// and https:// both with instances of www. or not.

However, I do not get a login or signup link since it's not included in the actual html of the page, and replacing links does so to header objects. I'd find some better way to do so. But here you'll see that the link replacement is working.

Code: Select all

<?php

require_once __DIR__.'/vendor/autoload.php';

use Curl\Curl;

$url = 'https://ebay.com';
$c = new Curl();
$c->setOpts([
	CURLOPT_FOLLOWLOCATION => true,
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_HEADER => false,
]);
$c->get($url);

$c->response = preg_replace_callback('/(?:http:\/\/(?:www.)?|https:\/\/(?:www.)?)|www./i', function ($m) {
	return 'http://mydomain.com/tracker.php?url=';
}, $c->response);

echo $c->response;
The Curl\Curl instance is using php-curl-class. I just like it better.

Here is it working
Image

Re: Why str_replace Not Working Properly With cURL?

Posted: Sun Jun 18, 2017 8:32 pm
by Pazuzu156
I probably should have mentioned, since the replacement I did no longer shows login/signup, it's likely something included in a script, and thus you cannot replace the values from there, as they're not loaded in just the response grabbed from cURL