Lassie Posted March 3, 2015 Share Posted March 3, 2015 I need to append some query strings to url's and am trying to use http_build_query. I have the following types of url to build where there are varaibles to append http://www.planningfinder.co.uk/search/near?postcode=EX23+9DZ&radius=5 http://www.crime-statistics.co.uk/postcode/EX23%209DZ http://api.zoopla.co.uk/api/v1/zed_index?area=TW14TR&output_type=outcode&api_key=1234k I have tried the last one first with the following code but I get an error unknown location form the response. Is this the right way to build query strings? $post_code=$_GET['post_code'];$key="1234k";//not a real key$outcode= "outcode";$fields = array('area'=>$post_code,'output_type=>$outcode,'api_key=>$key);$url = "http://api.zoopla.co.uk/api/v1/zed_index?" . http_build_query($fields, '', "&"); Quote Link to comment https://forums.phpfreaks.com/topic/295046-help-with-http_build_query/ Share on other sites More sharing options...
Lassie Posted March 3, 2015 Author Share Posted March 3, 2015 OK, I have the first one working, but I am still unsure about this format:-http://www.crime-statistics.co.uk/postcode/EX23%209DZ Would the same approach work as they are not value pairs? Quote Link to comment https://forums.phpfreaks.com/topic/295046-help-with-http_build_query/#findComment-1507348 Share on other sites More sharing options...
maxxd Posted March 3, 2015 Share Posted March 3, 2015 It looks like that's being routed on the recipient end. So what you've got is the equivalent of www.crime-statistics.co.uk?postcode=EX23 9DZ. Although I'm not sure this will work properly with the space - I should think it should be url-encoded. Quote Link to comment https://forums.phpfreaks.com/topic/295046-help-with-http_build_query/#findComment-1507351 Share on other sites More sharing options...
Lassie Posted March 3, 2015 Author Share Posted March 3, 2015 Yes I guess so. What do you mean regarding url-encode? Also how would I then call the page? eg echo "<a target=blank href=" . $url . "</a>; Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/295046-help-with-http_build_query/#findComment-1507352 Share on other sites More sharing options...
maxxd Posted March 3, 2015 Share Posted March 3, 2015 URL encoding in PHP. And yes, your example is almost correct: echo "<a target='_blank' href='".$url."'>This Is Example Text</a>"; Note the single quotes around the attribute values and the closing '>' on the opening tag, plus closing quotation marks on the string. Quote Link to comment https://forums.phpfreaks.com/topic/295046-help-with-http_build_query/#findComment-1507354 Share on other sites More sharing options...
Lassie Posted March 3, 2015 Author Share Posted March 3, 2015 Hi, Thanks So does the uRL encode work like this. echo "".urlencode($url).""; Quote Link to comment https://forums.phpfreaks.com/topic/295046-help-with-http_build_query/#findComment-1507356 Share on other sites More sharing options...
cyberRobot Posted March 3, 2015 Share Posted March 3, 2015 So does the uRL encode work like this. echo "".urlencode($url).""; According to the documentation (http://php.net/manual/en/function.urlencode.php), the function returns a string. So it should work; give it a try. Quote Link to comment https://forums.phpfreaks.com/topic/295046-help-with-http_build_query/#findComment-1507357 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.