Why is PHP changing currency symbol when I fetch data using CURL? - php

I am fetching data from kickstarter campaign, when I view it from my browser it displays me "Euro" symbol but when I fetch html content of the same page using CURL it shows me "dollar" symbol. Why is that so ?
Below is my PHP code (using CURL module) :
<?php
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$data = curl_exec($ch);
return $data;
?>
I want it to display me correct currency symbols like if the project is in "USD" it should return me "USD" and same with "EUR".
For example below is link to a campaign which has "EUR" currency symbol but in CURL fetched data its changing to "USD" , why so ? , is PHP auto converting that based on my server settings ?
Example link : https://www.kickstarter.com/projects/35540661/new-colors-59-stainless-milanaise-loop-for-apple-w

Your useragent is setting the location to en-us
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
I guess that is the reason the currency is set to $.
I do not know how kickstarter defines which currency to use. Maybe the server the cURL request is comming from is located in the US and kickstarter is using the ipaddress to define the currency.

Related

Add proxy ip into HTTP_Request?

I am using http request for scrap webpage. so i am using the following code
$this->rq = new HTTP_Request();
$this->rq->addHeader(
'User-Agent',
'Mozilla/6.0 (Windows; U; Windows NT 6.0; ja; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1 (.NET CLR 3.5.30729)'
);
$this->rq->addHeader('Keep-Alive', 115);
$this->rq->addHeader('Connection', 'keep-alive');
$this->rq->setURL('my url');
$this->rq->sendRequest();
So now i need to send proxy ip into this request call.
Did you try $this->rq->setProxy(<proxy hostname>, <optional proxy port>, <optional username>, <optional port> ); ?

change location when file_get_contents

I use php file_get_contents to fetch a page from a location where people speak Chinese. I did test if I use browser directly to visit $path and the website detected my location and showed me this country's currency. Is it possible to let the browser think I am at united states? I tried send header like below but nothing change.
$opts = array(
'http'=>array(
'method' => "GET",
'header' => "Accept-language: en\r\n" .
// "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20120306 Firefox/3.6.28 ( .NET CLR 3.5.30729; .NET4.0E)\r\n"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13\r\n"
)
);
$context = stream_context_create($opts);
$html = file_get_contents($path, false, $context);
If the server that you want to load the page from detects your location by your IP you need to use a proxy. You can look for open proxies, or create one for yourself in AWS for example.

How to parse image feed with php

Using Wikipedia API link to get main image about some world known characters/events.
Example : (Stanislao Mattei)
This would show as following
Now my question
I'd like to parse the xml to get image url to be shown up
here is the code i'm willing to use if it right ~ thanks to ccKep ~
<?PHP
ini_set("user_agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
$url = "http://en.wikipedia.org/w/api.php?action=query&list=allimages&aiprop=url&format=xml&ailimit=1&aifrom=Stanislao Mattei";
$xml = simplexml_load_file($url);
$extracts = $xml->xpath("/api/query/allimages");
var_dump($extracts);
?>
It should gives results as array
how i can get among it the exact url of the image to be shown should be :
http://upload.wikimedia.org/wikipedia/en/a/a1/Stanislaus.jpg
to put it in html code
<img src="http://upload.wikimedia.org/wikipedia/en/a/a1/Stanislaus.jpg">
~ Thanks a lot
Did you try $xml->query->allimages->img->attributes()->url
Your code will look like this:
<?php
ini_set("user_agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
$url = "http://en.wikipedia.org/w/api.php?action=query&list=allimages&aiprop=url&format=xml&ailimit=1&aifrom=Stanislao Mattei";
$xml = simplexml_load_file($url);
$url = $xml->query->allimages->img->attributes()->url;
echo "URL: ".$url . "<br/>";
echo '<img src="'.$url.'">';
?>

Curl 400 error when using UserAgent

Why i'm getting sometimes this error?
**Bad Request**
Your browser sent a request that this server could not understand.
Apache Server at control.digitalcoding.com Port 80
When
$UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11";
everything works fine, but not with
Opera/7.52 (Windows NT 5.1; U) [en]
Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1
Mozilla/5.0 (Windows NT 6.1; rv:10.0.1) Gecko/20100101 Firefox/10.0.1
for example. What is the problem?
HtmlReciever.php
<?php
if(empty($_GET["Link"]))
{
echo "empty";
die;
}
$LinkToFetch = urldecode($_GET["Link"]);
$UserAgent = urldecode($_GET["UserAgent"]);
function iscurlinstalled()
{
if (in_array ('curl', get_loaded_extensions()))
{
return true;
}
else
{
return false;
}
}
// If curl is instaled
if(iscurlinstalled()==true)
{
$ch = curl_init($LinkToFetch);
curl_setopt($ch, CURLOPT_USERAGENT,$UserAgent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$HtmlCode = curl_exec($ch);
curl_close($ch);
}
else
{
$HtmlCode = file_get_contents($LinkToFetch);
}
echo $HtmlCode;
?>
I must say that i'm running RecieverHtml.php from another .php with GET like this
http://127.0.0.1/reciever/RecieverHtml.php?Link=http%3A%2F%2Fwww.digitalcoding.com%2Ftools%2Fdetect-browser-settings.html&UserAgent=Mozilla%2F5.0+%28Windows+NT+6.1%3B+rv%3A10.0.1%29+Gecko%2F20100101+Firefox%2F10.0.1%0D%0A
This depends on the server your request is sent to. If the server checks the user agent and allows only requests that match a limited/incomplete/outdated list of common browser user agents, the server might return a generic 400 status code.
If you don't have control over the server and want your script to work, use the user agent that works and forget about the others. The user agent you provide with your request is "wrong" anyway, as it is not Chrome doing the actual request but your server running your PHP script.
EDIT:
You can also pass the user agent of the browser that requests your PHP script by using the following code:
curl_setopt($ch, CURLOPT_USERAGENT, $_REQUEST['HTTP_USER_AGENT']);
Just keep in mind that the value might be empty or exotic (like. Lynx/2.8.8dev.3 libwww-FM/2.14 SSL-MM/1.4.1) and be rejected by the server.

Programmatically fetching definition of a word

I am writing a social app where people will use TAGs for organizing their articles. These tags are shared across the site and each tag needs to have some description with it.
I wonder if there is any way I can programmatically fetch it from a resource like wikipedia. (say the first paragraph).
The tags will be typically associated with brands products and services.
Yes you can
<?php
$contents = file_get_contents("http://en.wikipedia.org/wiki/PHP");
preg_match("/<p>(.*?)<\/p>/", $contents, $match);
echo $match[1];
?>
http://sandbox.phpcode.eu/g/45c56.php
EDIT: Looks like they don't like non-validated browser agents. You'll have to do it with curl
EDIT2: curl with browser agent:
<?php
$ch = curl_init("http://en.wikipedia.org/wiki/PHP");
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
preg_match("/<p>(.*?)<\/p>/", $contents, $match);
$match[1] = preg_replace("|\[[0-9]\]|", "", strip_tags($match[1]));
echo (($match[1]));
?>
http://sandbox.phpcode.eu/g/ad578.php

Categories