I am using a command like
$page = file_get_contents($url);
where
$url = "http://www.site.com/search/index.cfm?tab=names&workername=firstname lastname";
When the url is typed directly in the browser chrome adds a %20 in between firstname and lastname and the website handles things properly.
However when I use $url with a space, file_get_contents grabs only results that match the firstname and isn't aware that workername = "firstname lastname"
When I explicitly add "%20" in between it returns NULL...
What's the work around?
Thanks guys!
A few problems:
I hope this is just because you quick-typed it here, but you need to have the string in single quotes.
There is no query string for the URL because you never start it with the ?, so no, neither of those variables will exist in $_GET.
You do have to redefine spaces as %20 for URLs.
$url = 'http://www.site.com/search/?tab=names&workername=firstname%20lastname';
^ ^ ^^^ ^
Edit:
It is possible that the website you're trying to query is ignoring them as GET variables. Have you tried adding this code to explicitly say it is a GET request?
$options = array(
'http' => array('method' => "GET", 'header' => "Accept-language: en\r\n")
);
$context = stream_context_create($options);
file_get_contents($url, false, $context);
You need to urlencode your text:
$fixed= urlencode($workername);
$url = 'http://www.example.com/search/?tab=names&workername='.$fixed;
Related
I would like to remove a querystring parameter from a url that may contain multiple. I have succeeded in doing this using str_replace so far like this:
$area_querystring = urlencode($_GET['area']);
str_replace('area=' . $area_querystring, '', $url);
preg_replace also works, like this:
preg_replace('~(\?|&)area=[^&]*~', '$1', $url)
Either works fine for most URLs. For example:
http://localhost:8000/country/tw/?area=Taipei%2C+Taiwan&fruit=bananas
Becomes:
http://localhost:8000/country/tw/?&fruit=bananas
However, if the querystring contains an apostrophe html entity, nothing happens at all. E.g.:
http://localhost:8000/country/cn/?area=Shanghai%2C+People%27s+Republic+of+China&fruit=bananas
The %27 part of the url (an apostrophe) seems to be the cause.
To be clear, I don't wish to remove all of the URL after the last /, just the area querystring portion (the fruit=bananas part of the url should remain). Also, the area parameter does not always appear in the same place in the URL, sometimes it may appear after other querystring parameters e.g.
http://localhost:8000/country/tw/?lang=taiwanese&area=Taipei%2C+Taiwan&fruit=bananas
You can use the GET array and filter out the area key. Then rebuild the url with http_build_query. Like this:
$url = 'http://localhost:8000/country/cn/?area=Shanghai%2C+People%27s+Republic+of+China&fruit=bananas';
$filtered = array_filter($_GET, function ($key) {
return $key !== 'area';
}, ARRAY_FILTER_USE_KEY);
$parsed = parse_url($url);
$query = http_build_query($filtered);
$result = $parsed['scheme'] . "://" . $parsed['host'] . $parsed['path'] . "?" . $query;
You probably don't need urlencode() -- I'm guessing your $url variable is not yet encoded, so if there are any characters like an apostrophe, there won't be a match.
So:
$area_querystring = $_GET['area'];
should do the trick! No URL encoding/decoding needed.
The question seems quite simple, but I've tried everything that I've read and nothing worked
I have this example url: localhost/test/{"user":"test","password":"test"} so, that json is part of the url how can I add it? I tried the following things
$arrayVariable = array (
"Usuario" => "user",
"Clave" => "test"
);
$res = json_encode($arrayVariable);
but the answer of $res is the following
"{\"Usuario\":\"user\",\"Clave\":\"test\"}"
I've tried str_replace to remove backslashed but it didn't work, I tried the following two functions
$res = str_replace("\\","",$res)
$res = str_replace("\\\\","",$res)
but it didn't work because seems the backslash is part of the " quote
Edit: I can't change the url because is an external API so nothing I can do that way
You could use the urlencode() function.
$arrayVariable = array (
"Usuario" => "user",
"Clave" => "test"
);
$res = urlencode(json_encode($arrayVariable));
But i think you should re-think your logic since this is a very unusual thing to do.
I've a URL which becomes a query string with the http_build_query function.
But I have a parametertimestamp which I cannot edit. And × becomes a multiplication sign x.
Is there a workaround for this?
This is my array which gets passed to the http_build_query function.
$parameters = array(
"transaction_id"=>uniqid("FF-"),
"timestamp"=> time(),
"order_total"=>$_SESSION['total_price'],
"order_total_with_vat"=>$_SESSION['total_price'] * 1.21,
"order_vat"=>"21",
"payment_method"=>"ideal",
"payment_status"=>"1",
"customer_name"=>$_SESSION['customer_data']['naam'],
"customer_address"=>$_SESSION['customer_data']['address'],
"customer_city"=>$_SESSION['customer_data']['city'],
"customer_zipcode"=>$_SESSION['customer_data']['zipcode'],
"customer_country"=>$_SESSION['customer_data']['country'],
"customer_email"=>$_SESSION['customer_data']['email'],
"customer_telephone"=>$_SESSION['customer_data']['telephone'],
);
Output of url:
http://somedomain/subdir/someapi/order?transaction_id=FF-58e2451c5aea9×tamp=1491223836&order_total=156695&order_total_with_vat=189600.95&order_vat=21&payment_method=ideal&payment_status=1&customer_name=t&customer_address=t&customer_city=t&customer_zipcode=t&customer_country=t&customer_email=t%40t&customer_telephone=t&product%5B0%5D=5&product%5B1%5D=5&product%5B2%5D=5&product%5B3%5D=5&product%5B4%5D=5&product%5B5%5D=5&product%5B6%5D=5
Preferred output:
http://somedomain/subdir/someapi/order?transaction_id=FF-58e2451c5aea9×tamp=1491223836&order_total=156695&order_total_with_vat=189600.95&order_vat=21&payment_method=ideal&payment_status=1&customer_name=t&customer_address=t&customer_city=t&customer_zipcode=t&customer_country=t&customer_email=t%40t&customer_telephone=t&product%5B0%5D=5&product%5B1%5D=5&product%5B2%5D=5&product%5B3%5D=5&product%5B4%5D=5&product%5B5%5D=5&product%5B6%5D=5
http_build_query function:
case 'POST':
curl_setopt( $curlHandler, CURLOPT_POST, true );
$url .= '?' . http_build_query( $parameters );
break;
Currently it is working correctly. The issue is that when you echo your URL the sequence × will make your browser replace it with the multiplication symbol x. To echo it and show the correct way in the browser try this:
echo htmlspecialchars($url);
This will display the desired URL.
#Novice was right, he commented on my question:
It's just a display issue in browser but your curl data will go just like what you have shown in your preferred output , no need to add anything extra just hit and go! – Novice
When I checked the apache access.log I could see the preferred query string posted.
The following code lives on one of my servers:
$curl = curl_init();
$url = "http://www.example.com/controller/action?param1=" . $value1 . "¶m2=" . $value2;
$url = str_replace(" ","%20",$url);
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
));
$result = curl_exec($curl);
Most of the time, this works just fine. However, today in example.com's access logs, I noticed an entry where the second parameter was completely missing. Not just the value, the entire parameter. So the log line was
GET /controller/action?param1=36838242 HTTP/1.1
I can't think of any condition what would cause param2 to be completely missing from the querystring. However, obviously it happened. And there is only one block of code that makes this curl call, so this is definitely the code responsible for the access log entry.
So my question is, under what condition could part of a concatenation fail, but have the rest of the code continue running? Since the code works 99% of the time, I'd love to write it off as a fluke, but this is really bugging me.
As others have pointed out, the string concatenation cannot fail.
I'm going to guess that $value1 contains a some non-printable character that is not being properly encoded. Your str_replace is only handling spaces. You would be much safer doing something like this:
$params = array( 'param1' => $value1,
'param2' => $value2 );
$url = 'http://www.example.com/controller/action?' . http_build_query($params);
And dropping the current call to str_replace.
I've got an issue whereby PHP is escaping where I really don't want it to in this code:
$url_ = stripslashes(((substr(strtolower($url),0,7)!="http://")? "http://".$url:$url));
$host = $this->googleDomains[mt_rand(0,count($this->googleDomains)-1)];
$target = "/search?";
$querystring = sprintf("client=navclient-auto&ch=%s&features=Rank&q=%s",
$this->CheckHash($this->HashURL($url_)),urlencode("info:".$url_));
$contents="";
$this->debugRes("host", $host);
$this->debugRes("query_string", $querystring);
$this->debugRes("user_agent", $this->userAgent);
thus producing a URL like this which causes the script to fail:
{"urls":[{"url":"hostcule.com","converted_url":"http:\/\/toolbarqueries.google.com\/search??client=navclient-auto&ch=74451333464&features=Rank&q=info%3Ahttp%3A%2F%2Fhostcule.com"}]}
How do I stop it?
Magic Quotes are Off.
Here's the $url comes from:
foreach (preg_split('#[\r\n]+#', $_POST['urls']) as $url) {
$url = trim($url);
if ($url)
$_SESSION['converted_urls'][] = array('url' => $url, 'converted_url' => $pr->GetPR($url, true, true));
}
At this stage, $_POST['urls'] looks like:
{"urls":[{"url":"hostcule.com","converted_url":"http:\/\/www.google.com\/search??client=navclient-auto&ch=74451333464&features=Rank&q=info%3Ahttp%3A%2F%2Fhostcule.com"}]}
whilst $url looks like
{"urls":[{"url":"hostcule.com","converted_url":"http:\/\/www.google.com\/search??client=navclient-auto&ch=74451333464&features=Rank&q=info%3Ahttp%3A%2F%2Fhostcule.com"}]}
There is nothing in that code that would produce the code you quote.
My suspicion is that $url already contains the garbled http\/\/, and therefore your http:// recognizing mechanism never triggers.
You need to step back and look where $url comes from. There is where your problem will be.
The code you have there doesn't do any escaping at all. You'll need to post what you do to that $url_ after this line.
use ' instead of "