【问题标题】:Find and Replace Curl Output PHP查找和替换卷曲输出 PHP
【发布时间】:2019-03-27 20:42:58
【问题描述】:

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

即)卷曲到 mywebsite1.com/myfavoritestreams > 检索“mywebsite1.com/stream1”

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

using this stackoverflow 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 不工作还是输出不是你所期望的?
  • 您似乎将您的 curl 响应称为文件名? $newOutput = str_ireplace("mywebsite1", "mywebsite2", $filename)
  • 为什么其他网站会在您的服务器上返回文件名?

标签: php regex curl explode


【解决方案1】:

好的,所以我认为您不能使用 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);

【讨论】:

  • 优秀。我现在明白了!
猜你喜欢
  • 1970-01-01
  • 2018-05-02
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
相关资源
最近更新 更多