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);
Related
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;
}
I am using cURL instead of file_get_contents, which is working fine on the URL until I used a GET request variable in place of the city on the URL below.
Using cURL on the following: (Works)
$url = 'http://www.weather-forecast.com/locations/London/forecasts/latest';
This works fine, however when substituting 'London' with a variable $city:
URL: example.com/weather.php?city=London
$city = $_GET['city'];
$city = ucwords($city);
$city = str_replace(" ", "", $city);
$url = 'http://www.weather-forecast.com/locations/".$city."/forecasts/latest';
I get an error: The page you are looking for doesn't exist (404)
What am I doing wrong in my cURL function? This seems to work perfectly with file_get_contents, is there something I am missing?
cURL function
function curl_get_contents($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$contents = curl_get_contents($url);
echo $contents;
Please replace double quotes with single quotes in url,
$url = 'http://www.weather-forecast.com/locations/'.$city.'/forecasts/latest';
Task: We have wikipedia English page and need to retrieve the same page address in Japanese.
suggested to parse http://en.wikipedia.org/wiki/Mini 4wd?action=raw results (there are other languages links in the bottom), but this way is too inefficient. Are there any other ways is the one real option?
We found some API in Wiki that seems fine for single word. but for two words like - Kamen rider, mini 4wd ... it doesn't work.
My code is not working
$url = 'https://en.wikipedia.org/w/api.php?action=query&prop=langlinks&format=json&lllimit=100&llprop=url&lllang=ja&titles=Kamen rider';
$url = rawurldecode(urlencode($url));
echo $url;
// outputs: https://en.wikipedia.org/w/api.php?action=query&prop=langlinks&format=json&lllimit=100&llprop=url&lllang=ru&titles=Mini+4wd
// and then the rest your logic whatever it is the rest
$header[] = "Accept: application/json";
$header[] = "Accept-Encoding: gzip";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$response = json_decode(curl_exec($ch));
/* echo '<pre>';
print_r($response);
echo '</pre>'; */
exit;
Two words doesn't work because its not properly formatted. Kamen<space>rider and mini<space>4wd has spaces. You need it to be converted first. Consider this example:
$url = 'https://en.wikipedia.org/w/api.php?action=query&prop=langlinks&format=json&lllimit=100&llprop=url&lllang=ru&titles=Mini 4wd';
$url = rawurldecode(urlencode($url));
echo $url;
// outputs: https://en.wikipedia.org/w/api.php?action=query&prop=langlinks&format=json&lllimit=100&llprop=url&lllang=ru&titles=Mini+4wd
// and then the rest your logic whatever it is the rest
$contents = file_get_contents($url);
$contents = json_decode($contents, true);
// echo '<pre>';
// print_r($contents);
// echo '</pre>';
Sample Fiddle
Kindly try this code it works. Your $keywords = 'Mini 4wd';
$url = 'https://en.wikipedia.org/w/api.php?action=query&prop=langlinks&format=json&lllimit=100&llprop=url&lllang=ja&titles='.$keywords.'&redirects=';
$url1 = rawurldecode(urlencode($url));
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $url1);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// $output contains the output string
$output = curl_exec($ch);
$info = curl_getinfo($ch);
$exec = explode('"url":"',$output);
$exe = explode('",',$exec[1]);
$URL = $exe[0];
Output :
<p>Wikipedia Help here as <?php echo $URL;?></p>
I have to post values having single quote inside the string to a url using curl, single quote automatically stripped I think, how do I properly include single quote in strings while using curl,
$url = "test.com/req?a=10&b='1010','1012'";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
Use urlencode.
$url = "test.com/req?a=10&b=" . urlencode("'1010','1012'");
This way you can build query string of any complexity:
$url = 'test.com/req';
$query = array(
'a' => 10,
'b' => "'1010','1012'"
);
$url .= '?'. http_build_query($query);
// will produce this
// test.com/req?a=10&b=%271010%27%2C%271012%27
I tried to use JSON decode to get the youtube API feed. However, when I paste the JSON result in http://www.jsonlint.com/ I noticed something like
"media$group": {
"media$category": [
Unfortunately some symbols are rejected by php. Here is my code, I tried to remove this $ symbol, but maybe not success. How do I solve this?
$url = 'http://gdata.youtube.com/feeds/api/videosq=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
...
}
Your problem is the usage of PHP identifiers to access the contents. The simplest solution here would be to get an array instead of an object:
$data = json_decode ( $json , $assoc = true );
This allows access to fields with:
echo $result['media$group']['media$description'];
If you want to keep the object syntax, that's possible with this kludge:
echo $result->{'media$group'}->{'media$category'};
(But arrays are safer here. You don't get a fatal error should the format change and properties be absent.)
This work:
<?php
$url = 'http://gdata.youtube.com/feeds/api/videos?q=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
var_dump($result);
}
?>