小编cat*_*pus的帖子

PHP在URL中使用cURL和GET请求

我正在使用cURL而不是file_get_contents,这在URL上正常工作,直到我在下面的URL上使用GET请求变量代替城市.

在以下内容上使用cURL :(作品)

$url = 'http://www.weather-forecast.com/locations/London/forecasts/latest';

这很好,但是用"变量"替换"伦敦"时$city:

URL: example.com/weather.php?city=London

$city = $_GET['city'];
$city = ucwords($city); 
$city = str_replace(" ", "", $city);

$url = 'http://www.weather-forecast.com/locations/".$city."/forecasts/latest';
Run Code Online (Sandbox Code Playgroud)

我收到一个错误: The page you are looking for doesn't exist (404)

我在cURL函数中做错了什么?这看起来很完美file_get_contents,有什么我想念的吗?

cURL功能

function curl_get_contents($url)
{
$ch = curl_init();
$timeout = 5;

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$data = curl_exec($ch);

curl_close($ch);

return $data;
}

$contents = curl_get_contents($url);
echo $contents;
Run Code Online (Sandbox Code Playgroud)

php url curl get

6
推荐指数
1
解决办法
8211
查看次数

PHP使用for循环遍历数组

我正在使用for循环来循环遍历数组,并且有一个我到目前为止无法弄清楚的问题.

我有3个循环,在循环中是常见的颜色名称.使用第一个for循环我遍历所有3个循环并找到常见的颜色名称,这很好.

第二部分是我难以理解的方法,如何将公共值数组分配到另一个数组中以显示这些常用值.

我知道我可以使用foreach循环来实现如下所示的技巧,但我试图看看如何使用for循环来做到这一点.

我怎样才能做到这一点?(不使用array_intersect)

代码:(这循环遍历所有数组并给出我的共同值)

$array1 = ['red', 'blue', 'green'];
$array2 = ['black', 'blue', 'purple', 'red'];
$array3 = ['red', 'blue', 'orange', 'brown'];

$value = [];

$array_total = array_merge($array1, $array2, $array3);

$array_length = count($array_total);

for ($i = 0; $i < $array_length; $i++) {
    if (!isset($value[$array_total[$i]])) {
        $value[$array_total[$i]] = 0;
    }

    $a = $value[$array_total[$i]]++;
}
//print_r($value); -- Array ( [red] => 3 [blue] => 3 [green] => 1 [black] => 1 [purple] => 1 [orange] => 1 [brown] => 1 ) …
Run Code Online (Sandbox Code Playgroud)

php arrays foreach loops for-loop

2
推荐指数
1
解决办法
3264
查看次数

标签 统计

php ×2

arrays ×1

curl ×1

for-loop ×1

foreach ×1

get ×1

loops ×1

url ×1