查找并替换卷曲输出 PHP

Find and Replace Curl Output PHP

我有两个网站。我想转到网站 #1 上的一个页面来检索 html 代码,该代码具有指向我最喜欢的抽搐流的链接,使用他们的 api 嵌入到我的页面中。使用此 curl 输出,我想查找并替换我的网站 #1 的迭代并将其替换为我的网站 #2。

即)卷曲到 mywebsite1。com/myfavoritestreams > 检索 "mywebsite1.com/stream1"

我想查找、替换和显示 "mywebsite2.com/stream1"。

using this Whosebug questions as a guide 我试过这个的变体但没有成功。只是错误消息,它需要一个有效的字符串和路径。

感谢所有帮助。

$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);
}

好的,我认为您不能使用 curl 响应正文作为文件名。 你可以这样做。

$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);