douyingbei1458 2016-07-10 06:14
浏览 169

这个URL需要urlencoding在哪里?

I hosted a website i worked on on amazon web services, and for some reason some things dont work compared to when I run it locally on localhost.

of those things are the

if (array_key_exists("error", $json))

function and

the file_get_contents function.

Ive commented out the array key exists part and that solved the issue, at least for that part of logging in, until i get to the view documents page where a slim application error displays

failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

and the issue for that lies in the viewdocspage.php file, particularly this code:

<?php
                 $raw = file_get_contents("http://cosoft.us-east-1.elasticbeanstalk.com/cosoft/mywiki/api.php?action=query&list=allpages&format=json");
                 $pages_response = json_decode($raw, true);
                 $pages_array = $pages_response["query"]["allpages"];
                 $page_titles = [];

I looked and read up on many threads that the issue lies with the file_get_contents in which the URL contains special characters, such as spaces (which mine doesnt have) and so needs to be encoded, using urlencode (or rawurlencode..?)

now ive tried encoding the whole url like this:

$raw = file_get_contents(urlencode("http://cosoft.us-east-1.elasticbeanstalk.com/cosoft/mywiki/api.php?action=query&list=allpages&format=json"));

but that resulted in this error: slim error

Message: file_get_contents(http%3A%2F%2Fcosoft.us-east-1.elasticbeanstalk.com%2Fcosoft%2Fmywiki%2Fapi.php%3Faction%3Dquery%26list%3Dallpages%26format%3Djson): failed to open stream: No such file or directory


I figured that this may happen since I read that not all the URL should be wrapped by this encoding, but heres where Im stuck: which part of the url do i use the encoding on? the only special characters i keep coming accross regarding this error is spaces, but i dont have any spaces, so its something else which i dont know what it is...

Help is appreciated, thanks!

  • 写回答

2条回答 默认 最新

  • dongliao2241 2016-07-10 06:35
    关注

    You would just need to url_encode the parameters. Say you had a value $value='My name is earl' If you wanted to pass this value as a parameter in your url http://somesite.com/?name=$value there would be spaces in the value that is url_encoded. So if you encode it as 'http://somesite.com/?name='.urlencode($value), when this is encoded the value will turn into My+name+is+earl

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大