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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试