I am trying to fetch IP details of the user from the following url:
http://freegeoip.net/json/186.80.156.123
Now if you open up the above URL, you will see that the city parameter has an weird character in place of an accented character...how can I fix it before displaying in php?
my code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://freegeoip.net/json/".trim($user->ip_address));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_out = curl_exec($ch);
curl_close($ch);
$jout = json_decode($curl_out);
echo $jout->city.", ".$jout->region_name.", ".$jout->country_name;
It is encoded in UTF-8 but you are interpreting it as ISO-8859-1.
Either set the appropriate options, or just run the $curl_out value through utf8_decode().
Related
I have the following php:
$ch = curl_init("http://domain/blah/".rawurlencode("foo bar"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$result = curl_exec($ch);
print_r ($result);
curl_close($ch);
That works as it should, but when I change foo bar to foo\r\nbar or foo\nbar or drop the rawurlencode and just send foo%0D%0Abar or foo%0Abar, I get a Bad Request - Invalid URL error.
If I try:
$ch = curl_init("http://domain/blah/foo\r\nbar");
I get no error, but nothing happens.
I can put some other characters in the url and have them replaced with newlines, but that seems a bit of a workaround. I'm looking for the right way to do this.
I'm using php 5.6.7
I have the following php function:
function checkJQL($filter) {
$auth = "account:password";
$URL='http://jira/rest/api/latest/search?jql='.$filter;
echo $URL;
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Tell it to always go to the link
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$URL);
//Set username and password
curl_setopt($ch, CURLOPT_USERPWD, $auth);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
echo $result;
// Will dump a beauty json :3
var_dump(json_decode($result, true));
echo "DONE";
}
When I call this with $filter = "" It works fine, outputs everything. As soon as I insert a proper JQL query, it fails. When I enter random garbage, it works (As in I get a invalid input message), but when it's proper JQL it never works.
When I copy and paste the URL that I echo into the browser it works.
An example filter I used:
"Target" = "Blah"
When I think about it, I don't actually need this to work, I just need it to know when the input isn't JQL (which it does). But I'm really curious now. Anyone have ideas on what it might be?
You should URL-encode the $filter.
The reason why it works with empty or random strings is because they don't have any challenging characters that need to be URL-encoded.
The reason why the URL works in the browser but not in the script is because the browser does the URL encoding.
So do the following:
$URL='http://jira/rest/api/latest/search?jql='.urlencode($filter);
Link to urlencode: http://php.net/manual/en/function.urlencode.php
I cannot able to post data its contain the special characters through curl ,Any solution ?
first code is my curl function ,second code is my data need to pass
$communication_data_string = 'token='.json_encode(array(
"activityTypeId"=>12,
"activityId"=>5,
"userID"=> 10,
"partyID"=>20,
"message_area_name"=>("this is my test data with special cheractors&&!###$%$%%*)++")
)
);
echo(datPostingCURL($url, $communication_data_string));
?>
this seems to not returning anything if the messageData contains the special character's
Try urlencode
or
How about using the entity codes...
# = %40
& = %26
change the line
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
to
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($data_string));
the receive it in following way as you are not using any specific post variable
$data = urldecode(file_get_contents('php://input'));
Check the following code. I tested it in my local system. In my www director I created a folder named "test" for keeping the code. Then I created 2 files in that "test" folder. One file for sending the request and one for processing the request.
1. curl.php
<?php
function datPostingCURL($url, $data_string)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
return $result;
}
$url = "http://localhost/test/curl_post.php";
$communication_data_string = 'token='.json_encode(array(
"activityTypeId"=>12,
"activityId"=>5,
"userID"=> 10,
"partyID"=>20,
"message_area_name"=>base64_encode("hai ..this is my test data .!###$%$%%*)++")
)
);
echo(datPostingCURL($url, $communication_data_string));
?>
2. curl_post.php
<?php
print_r(json_decode($_POST['token']));
$details = json_decode($_POST['token']); var_dump($details);
echo base64_decode($details->message_area_name);
?>
Note : While using special characters you should encode that string. I tried to convert it with htmlspecialchars(). But it didn't worked. So I used base64 encoding method here.
I am getting the same error for this, please suggest
$url="http://domain.com/manage/File Name.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url); // get the url contents
$data = curl_exec($ch); // execute curl request
curl_close($ch);
echo $data;
This error come, when your curl url contains white spaces. you have to encode url for remove white space.
$base_url = "http://domain.com/manage/";
$url = "File Name.xml";
$ch = curl_init();
$final_url = $base_url . curl_escape($ch, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url); // get the url contents
$data = curl_exec($ch); // execute curl request
curl_close($ch);
echo $data;
As describe in the comment, your URL contains not encoded characters (spaces).
Solution
Encode your URL when setting CURLOPT_URL:
curl_setopt($ch, CURLOPT_URL, urlencode($url));
You could also use curl_escape() to encode the query string part.
References
answer of cURL having issues handling URL Source with colons.
You need to encode your URL before sending the request
<?php
$url=urlencode("http://domain/file name.xml");
?>
urlencode
Answers here suggest using url_encode or curl_escape functions. If you want to be RFC 3986 compliant, use rawurlencode() function instead. This helped with a curl request in PHP 7. Hoep it helps others.
I'm working on a web solution that allows user to view an online sqlite3 format file (e.g. http://xxxxxx.AgentStatistic.csv.sqlite3). Now I'm using php, and successfully get the resource with codes:
$url ="http://xxxxx.AgentStatistic.csv.sqlite3";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
This returns the binary code, which can be viewed like this, a long string (with unreadable code):
SQLite format 3# -â! iiƒSS…tableLitevsocial_reports_agentStatisticsLitevsocial_reports_agentStatisticsCREATE TABLE Litevsocial_reports_agentStatistics (reportTime,conversationId,dayPeriod,weekPeriod,monthPeriod,yearPeriod,agent,groupUser,dayLogin,weekLogin,monthLogin,yearLogin,assignedTo,assignedBy,composePublish,composeDraft,replyPublish,replyDraft,closed,waitingTime,slaReply,slaReplyOutside,slaClosed,slaClosedOutside) Î ûöñìçâÝØÓÎ\S J A8/& /˜7Öu³Rñ/_ 3 +2013-06-03 17:43:3972579thisYearChris 00002000000112080160001_ 3 +
.....(all rows with unreadable code like the first line)
The result includes the information I needed but is not readable and in bad format.
My question: is there a method to properly decode the sqlite3 resource based on my existing code? Or there are some other solutions in other language?