CURLOPT_URL codification with PREG_REPLACE value - php

When i get the $url varible by preg_replace feedback it doesn't work but it i specify the $url value with the commented line it works. What's wrong in my code please? Many thanks.
$content = preg_replace('/(plugin_[^ ]+)/', getPlugin('$1'), $content);
function getPlugin($plugin) {
$url = "http://".$_SERVER['HTTP_HOST']."/{$plugin}.php?language=en";
//$url = "http://".$_SERVER['HTTP_HOST']."/plugin_contact.php?language=en";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

My solution
if (preg_match("/(plugin_[^ ]+)/", "$content", $match)) {
$content = preg_replace('/(plugin_[^ ]+)/', getPlugin($match[0]), $content);
}

Your original code doesn't work because getPlugin() runs before preg_replace(). It is called with the literal '$1' as argument, there is nobody to replace $1 with something else because preg_replace() is not involved.
I suppose you are refactoring a call to preg_replace() that uses the deprecated e(PREG_REPLACE_EVAL) modifier:
$content = preg_replace('/(plugin_[^ ]+)/e', 'getPlugin("$1")', $content);
In order to remove the /e modifier you can use preg_replace_callback():
$content = preg_replace_callback('/(plugin_[^ ]+)/', 'getPlugin', $content);
function getPlugin(array $matches) {
$plugin = $matches[1];
$url = "http://".$_SERVER['HTTP_HOST']."/{$plugin}.php?language=en";
//$url = "http://".$_SERVER['HTTP_HOST']."/plugin_contact.php?language=en";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

Related

Replace string in raw data response is not working

Using PHP CURL i'm calling one URL and i'm getting some response from one page,Now I need to replace one string from that response but it's not working,please check my code below.
$url = "My URL";
$url1 = $url1 = str_replace(' ', '%20', $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$raw_data1 = curl_exec($ch);
curl_close($ch);
$raw = str_replace('#', 'Test', $raw_data1);
echo $raw;
You are trying to replace special character,My suggestion is you should try to use preg_replace() instead of str_replace()
Try the below example:
$url = "My URL";
$url1 = $url1 = str_replace(' ', '%20', $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$raw_data1 = curl_exec($ch);
curl_close($ch);
$raw = preg_replace("([#]+)", "Test", $raw_data1);
echo $raw;
To replace a pattern of string always use preg_replace() and in your case the response is coming from other source so better way to replace the string is to find define a pattern and replace Using preg_replace()
This is what you need preg_replace("([#]+)", "test", $raw_data1);

Separating explode array from URL string

I'm using BulkSMS which is a SMS service and I want to get how many messages I have left using the URL response.
This is the URL (with password removed):
https://www.bulksms.co.uk/eapi/user/get_credits/1/1.1?username=<username>&password=<password>
This then outputs something similar to:
0|2000.00
According to the documentation the first part refers to error messages and the second part refers to no. of messages remaining: status_code|status_description
So using cURL and explode I can get the URL response split via an array, but my question is how do I output the 2000.00 (status_description) as I would only want the 0 (status_code) for error checking?
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
$array = explode('|', $data);
print_r($array);
}
If it helps this is what the function outputs:
Array ( [0] => 0 [1] => 2000.00 )
Also I know I could use substr and strpos to get the remaining messages as shown below, but I would like to use the status_code for error checking.
$remaining_sms = substr($data, strpos($data, "|") + 1);
echo $remaining_sms;
If you just want the second part of the pipe delimited message then just return that part of the array, given you know the index...
This will output what you want
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
$array = explode('|', $data);
print_r($array[1]);
}
Or alternatively you could return it and then parse it in your code later
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
$array = explode('|', $data);
return $array;
}
Either way I don't get why you're substring-ing when you've already gotten it

Rewrite file_get_content function with cURL

So I have this function which I'm trying to make with cURL because of server securities file_get_contents() is disabled.
$url ='https://example.com' .
'/b/'.urlencode($this->user_ID).'/o/'.urlencode($this->ID);
$url=$url.'/h/'.urlencode($hash);
$number=rand(0, 10)/10 ."";
$url=$url.$number;
$html = file_get_contents($url);
preg_match('/<td valign="bottom" class="sum">(.*?)<\/td\>/', $html , $matches);
return $matches[0];
What I have now is this
$url ='https://example.com' .
'/b/'.urlencode($this->user_ID).'/o/'.urlencode($this->ID);
$number=rand(0, 10)/10 ."";
$url=$url.$number;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$matches = curl_exec($ch);
curl_close($ch);
$html = file_get_contents($url);
preg_match('/<td valign="bottom" class="sum">(.*?)<\/td\>/', $html , $matches);
return $matches[0];
Is it something like this or I'm totally on wrong way? And how to change now this line - $html = file_get_contents($url); ?
You must remove the call to file_get_contents after the curl, you already have the response from the curl request.
replace this
$matches = curl_exec($ch);
with this
$html = curl_exec($ch);
Then do preg_match on the response
// $html = file_get_contents($url); this line is not needed
preg_match('/<td valign="bottom" class="sum">(.*?)<\/td\>/', $html , $matches);
return $matches[0];
Finally, inspect the contents of $matches and $matches[0]

PHP - get json data from url

I'm trying to load json data from this url =
http://api.opencagedata.com/geocode/v1/json?query=48.84737%2C2.28605&pretty=1&no_annotations=1&no_dedupe=1&key=b61388b5a248b7cfcaa9579ed290485b
Using file_get_contents works with other json urls but this one is strange. It returns only "{" the first line. Strlen gives 1480 which is right.Substr(2,18) gives "documentation" which is right too. But still i can't echo the entire text. Maybe there's some way to read the text line by line and save it in another string ? The entire text is still fully loaded in the textfile
Here's the php code i tried
<?php
$url = file_get_contents("http://api.opencagedata.com/geocode/v1/json?query=48.84737%2C2.28605&pretty=1&no_annotations=1&no_dedupe=1&key=b61388b5a248b7cfcaa9579ed290485b");
$save = file_put_contents("filename.txt", $url);
echo $url;
?>
Also tried this function but still same.
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
You can get return value with json_decode.
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data,true);
}

PHP: Get link address from redirected URL

How can I get the link address after a URL has been redirected?
Take for example this URL: http://www.boligsiden.dk/viderestilling/992cff55882a40f79e64b0a25e847a69
How can I make a PHP script echo the final URL? (http://www.eltoftnielsen.dk/default.aspx?side=sagsvisning&AutoID=125125&DID=140 in this case)
Note: The following solution isn't ideal for high traffic situations.
$url = 'http://www.boligsiden.dk/viderestilling/992cff55882a40f79e64b0a25e847a69';
file_get_contents($url);
preg_match('/(Location:|URI:)(.*?)\n/', implode("\n", $http_response_header), $matches);
if (isset($matches[0]))
{
echo $matches[0];
}
Here's what happens: file_get_contents() redirects and downloads the target website but writes the original response header into $http_response_header.
the preg_match tries to find the first "Location: x" match and returns it.
use this
<?php
$name="19875379";
$url = "http://www.ikea.co.il/default.asp?strSearch=".$name;
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$header = curl_exec($ch);
$redir = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
//print_r($header);
$x = preg_match("/<script>location.href=(.|\n)*?<\/script>/", $header, $matches);
$script = $matches[0];
$redirect = str_replace("<script>location.href='", "", $script);
$redirect = "http://www.ikea.co.il" . str_replace("';</script>", "", $redirect);
echo $redirect;
?>
enter link description here

Categories