2

I have two websites. I would like to curl to a page on website #1 to retrieve html code which has links to my favorite twitch streams, embed in to my page using their api. Using this curl output, I would like to find and replace iterations of my website #1 and replace it with mywebsite #2.

Ie) Curl to mywebsite1.com/myfavoritestreams > retrieves "mywebsite1.com/stream1"

I want to find, replace and display "mywebsite2.com/stream1" instead.

using this stackoverflow questions as a guide I tried variations of this with no success. Just error messages that it expects a valid string and path.

All help is appreciated.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.mywebsite1.com/myfavoritestreams");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");


$headers = array();
$headers[] = "Host: www.wiz1.net";
$headers[] = "Connection: keep-alive";
$headers[] = "Upgrade-Insecure-Requests: 1";
$headers[] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36";
$headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
$headers[] = "Referer: http://www.mywebsite1.com";
$headers[] = "Accept-Encoding: gzip, deflate";
$headers[] = "Accept-Language: en-US,en;q=0.9";
$headers[] = "Cookie: __cfduid=d00d780044d6524dab736999f38359bba1534699518; _ga=GA1.2.1974576514.1534699525; _gid=GA1.2.795232845.1540262479";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$filename = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
$string_to_replace="mywebsite1";
$replace_with="mywebsite2";
replace_string_in_file($filename, $string_to_replace, $replace_with);

function replace_string_in_file($filename, $string_to_replace, $replace_with){
    $content=file_get_contents($filename);
    $content_chunks=explode($string_to_replace, $content);
    $content=implode($replace_with, $content_chunks);
    file_put_contents($filename, $content);
}
3
  • 1
    is the curl not working or is the output just not what you expected? Commented Oct 23, 2018 at 15:32
  • You appear to be calling your curl response as a file name? $newOutput = str_ireplace("mywebsite1", "mywebsite2", $filename) Commented Oct 23, 2018 at 15:51
  • Why would another website return a filename on your server? Commented Oct 23, 2018 at 15:52

1 Answer 1

2

Ok so I don't think you can use the curl response body as the file name. You can do something like this.

$body = curl_exec($ch);
$output = str_replace($string_to_replace,$replace_with,$body);
$filename = "/tmp/temp.txt" // here you need to put a valid path to a file
$file = fopen($filename,'w+');
fwrite($file,$body);
fclose($file);
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. I understand this now!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.