Asked  7 Months ago    Answers:  5   Viewed   97 times

I would like file_get_contents to get a url that looks like this: http://wapedia.mobi/sv/Gröt

The problem is that it requests (can't post entire link, sorry): ...wapedia.mobi/sv/Gr%C3%B6t which you can see has been urlencoded, that page does not give me any results.

How can I do this?

 Answers

91

According to the PHP manual, you must specifically encode a URL if it contains special characters. This means the function itself should do no special encoding. Most likely your URL is being encoded before being passed to the function, so pass it through urldecode first and see what happens.

Edit: You're saying the encoding is being messed up. Again the PHP manual specifically states that you need to encode urls prior to passing them to file_get_contents. Try encoding the URL, then passing it to the function.

$url = urlencode($url);
file_get_contents($url);
Friday, May 28, 2021
 
rorymorris
answered 7 Months ago
39

Finally found this method:

basename($_SERVER['REQUEST_URI']);

This will return all URLs with page name. (e.g.: index.php?id=1&name=rr&class=10).

Wednesday, March 31, 2021
 
Whakkee
answered 9 Months ago
18

Special thanks to user @dorad for helping me get this working correctly. This is the code that actually works. I suspect the original version I had was coded for an earlier version of PHP which will not work correctly for special characters in newer PHP versions. So without further ado, this is the final code that works:

<?php
//creates a image handle
$image = imagecreate( 700, 70 );

if(!empty($_POST["bgcolor"])){
$background = imagecolorallocate( $image,0, 0, 0);
}

else {
$background = imagecolorallocate( $image,255, 255, 255);
}

//GET COLORS FROM POST AND SPLIT INTO RGB FORMAT
$color  =  $_POST["color"];
$pieces = explode("-", $color);
 //COLORS
$color = imagecolorallocate($image, hexdec("0x".$pieces[0].""), hexdec("0x".$pieces[1].""),  hexdec("0x".$pieces[2].""));


$font = 'fonts/'.$_POST["font"].'';
$fontSize = "25";
$fontRotation = "0";
$str = utf8_encode_mix($_POST["name"]);

$regex = $str;
$replaced = preg_replace($regex,"%26",$str);


$tb = imagettfbbox(25, 0, $font, $str);
$x = ceil((700 - $tb[2]) / 2);

ImageTTFText($image, $fontSize, $fontRotation, $x, 50, $color, $font, $str);

header("Content-Type: image/PNG");
ImagePng ($image);
imagedestroy($image);

    function utf8_encode_mix($input, $encode_keys=false)
    {
        if(is_array($input))
        {
            $result = array();
            foreach($input as $k => $v)
            {                
                $key = ($encode_keys)? utf8_encode($k) : $k;
                $result[$key] = utf8_encode_mix( $v, $encode_keys);
            }
        }
        else
        {
            $result = utf8_encode($input);
        }

        return $result;
    }
?>

Calling page index2.php

<div style="padding:00px;">

<strong>Please complete this form to preview the font and color font your name Embroidery: </strong>
<br><br>


<form action="index2.php?page=embroidery" method="post" name="font" style="background-color:#EDFAFC;">

<input name="page" type="hidden" value="lab_coats_embroidery">
<?
function checkcolor ($color) {

if($color == $_POST["color"]){
    echo "checked";
}
}

//
?>


<table width="600" border="0" cellspacing="4" cellpadding="4">
  <tr>
    <td width="163">Please enter your name: </td>
    <td width="398">Line 1:
      <input name="name" type="text" value="<?=$_POST["name"]?>" size="40">
      <br />
      Line 2:
      <input name="name2" type="text" value="<?=$_POST["name2"]?>" size="40" />
<br />
      Line 3:
      <input name="name3" type="text" value="<?=$_POST["name3"]?>" size="40" /></td>
    <td width="18">&nbsp;</td>
    <td width="21">&nbsp;</td>
  </tr>
  <tr>
    <td>Please select a font: </td>
    <td><select name="font" id="font">
     <option value="<?=$_POST["font"]?>"></option>
      <option value="brush">Brush script size 14</option>
      <option value="athletic">Athletic swoosh</option>
      <!--<option value="diana">Diana script</option>-->
      <option value="brush738">Brush 738</option>
      <option value="helvetica">Helvetica narrow</option>
      <option value="homeward">Homeward</option>
      <option value="cheltenham">Cheltenham</option>
      <option value="athletic_narrow">Athletic Narrow</option>
      <option value="courier">Courier</option>
<?php /*?>      <option value="cancun">Cancun</option><?php */?>
    </select>
      Color:
      <select name="color" id="color">
        <option value="<?=$_POST["color"]?>"></option>
        <option value="00-00-00">Black</option>
        <option value="cc-00-00">Red</option>
        <option value="05-34-92">Royal Blue</option>
        <option value="13-2c-61">Navy</option>
        <option value="86-00-41">Burgundy</option>
        <option value="fd-08-c8">Fuchsia</option>
        <option value="73-b9-ff">Sky Blue</option>
        <option value="02-74-8c">Teal</option>
        <option value="02-4b-2d">Forest Green</option>
        <option value="43-07-71">Purple</option>
        <option value="ff-c6-00">Gold</option>
        <option value="dd-dd-dd">Silver</option>
        <option value="96-54-31">Bronze</option>
      </select></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="button" id="button" value="Preview My Name" /></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>

<div style="text-align:center;">
<!-- orig setting height 65px and no background positioning -->
<div style="background-image:url('dpimage.php?name=<?=$_POST["name"]?>&font=<?=$_POST["font"]?>&color=<?=$_POST["color"]?>'); background-repeat:no; background-position: center -20px; width:700px; height:40px;"></div>
<div style="background-image:url('dpimage.php?name=<?=$_POST["name2"]?>&font=<?=$_POST["font"]?>&color=<?=$_POST["color"]?>'); background-repeat:no; background-position: center -20px; width:700px; height:40px;"></div>
<div style="background-image:url('dpimage.php?name=<?=$_POST["name3"]?>&font=<?=$_POST["font"]?>&color=<?=$_POST["color"]?> '); background-repeat:no; background-position: center -20px; width:700px; height:40px;"></div>

</div>

</div>
Wednesday, March 31, 2021
 
njai
answered 9 Months ago
14

$cache_disqus is defined outside your function, and is therefore not accessible within the function.

Check out the PHP documentation on variable scope.

Saturday, May 29, 2021
 
IvanH
answered 7 Months ago
77

The URL you have specified is invalid. file_get_contents expects a valid http URI (more precisely, the underlying http wrapper does). As your invalid URI is not a valid URI, file_get_contents fails.

You can fix this by turning your invalid URI into a valid URI. Information how to write a valid URI is available in RFC3986. You need to take care that all special characters are represented correctly. e.g. spaces to plus-signs, and the commercial at sign has to be URL encoded. Also superfluous whitespace at beginning and end need to be removed.

When done, the webserver will tell you that the access is forbidden. You then might need to add additional request headers via HTTP context options for the HTTP file wrapper to solve that. You find the information in the PHP manual: http:// -- https:// — Accessing HTTP(s) URLs

Thursday, August 5, 2021
 
Anele
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share