gpt4 book ai didi

php - 使用 cURL 重定向?

转载 作者:可可西里 更新时间:2023-11-01 00:17:26 25 4
gpt4 key购买 nike

我正在尝试使用 cURL 执行重定向。我可以很好地加载页面,这不是问题,但是如果我加载说 google.com 没有图像加载并且网站无法正常工作(显然是因为它只是打印 HTML 而不是实际进行重定向)。

有什么方法可以使用 cURL 执行重定向吗?有点类似于......

header("Location: http://google.com");

...有效吗?

如有任何帮助,我们将不胜感激。

最佳答案

好吧,根据我的理解,OP 似乎希望将用户重定向到搜索结果 URL。使用 GoogleAPI 将是首选,要实现类似的目标,我会这样做:

<?php

$query = "firefox";
$apiUrl = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=".urlencode($query);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $apiUrl);
$content = curl_exec($ch);

$content = json_decode($content);

$luckyUrl = $content->responseData->results[0]->unescapedUrl;



header("Location: ".$luckyUrl);
?>

上面的代码就像“我感到幸运”......

关于php - 使用 cURL 重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4023894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com