Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Getting weather data from openweathermap fails. Here is my code:
$xml = new
SimpleXMLElement(file_get_contents('http://api.openweathermap.org/data/2.5/weather?q=london&mode=xml'));
$country = $xml->code->country;
$city = $xml->code->city;
echo "Country: $country<br/>City: $city";
When I echo I don't get anything at all. Help is appreciated!
The proper path to the country and city values are as follows:
$country = $xml->city->country;
$city = $xml->city['name'];
You may also need to remove the spaces in your URL, so the complete code would look like:
$xml = new SimpleXMLElement(file_get_contents('http://api.openweathermap.org/data/2.5/weather?q=london&mode=xml'));
$country = $xml->city->country;
$city = $xml->city['name'];
echo "Country: $country<br/>City: $city";
You may want to have a quick look over basic SimpleXML usage.
You are missing all the error message nor are you missing to check function return values before continuing further on:
Warning: file_get_contents(http://api.openweathermap.org/data/2.5/weather? q=london&mode=xml): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD_REQUEST
This means your HTTP request has failed because it's a bad request. Check if the URI is correct. If so, contact the API service provider for your support options.
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML'
Your script has a fatal error. It not only does not output anything, it even stopped to halt before it reached it's planned end. That is because you think everything must always work. Instead also check for error messages.
This has been outlined in a previous question here:
How to get useful error messages in PHP?
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi guys I'm following this tutorial and I'm getting an extremely strange error message in my PHP when I try and run it in the web browser. The code is as follows:
<?php
// Pull in the NuSOAP
require_once('nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
//(MyService is name of our service)
$server----->configureWSDL('MyService', 'urn:MyService');
// Character encoding
$server->soap_defencoding = 'utf-8';
//-------------------------------------------------
//Registrations of our functions
//-------------------------------------------------
//Our web service functions will be here.
//-------------------------------------------------
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
Which is exactly as it's written in the tutorial, yet I keep getting this error message every time I run the PHP file:
UPDATE now I'm getting this error
Parse error: syntax error, unexpected T_DEC in
/home/a1335235/public_html/MyService.php on line 8
Can anyone figure out why?
As Mark Baker said in the comments (and I'll delete this if he posts first), your code is:
code require_once();
First and foremost code is not PHP. This should be require_once and you can see this in the PHP Manual. Secondly, require_once is a language construct. It's not a function call, so you don't need the (). You should have:
require_once 'nusoap.php';
The syntax error is telling you exactly what line the problem is on, so read it and google that bit of code to see how others are doing it and where you are going wrong in the future.
Now your issue is on line 8:
$server----->configureWSDL
This is not valid PHP either, unless you've heavily modifed the source code of the language, which you haven't. Change this to:
$server->configureWSDL()
That's how you call methods on objects. You should be reading the manual on Objects to see how this works.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Hey there i have this snippet:
$profile_pic = imagecreatefromjpeg("upload_Main/".$user[1].".jpg");
This function works (upload_Main: folder:777 ; files:644)
But this snippet:
$profile_pic = imagecreatefromjpeg("upload_Sub/".$user[1].".jpg");
Doesnt work (upload_Sub: folder:777 ; files:644).
They have exactly the same files and the same right´s, so why does the second one not work? Any idea´s? thanks and greetings!
imagecreatefromjpeg():
Returns an image resource identifier on success, FALSE on errors.
I had it return False on an image that was manually re-named to a JPG, when it was actually a PNG.
I got an error saying:
'not a valid JPEG file'
You should look to see what error you get when performing that function, That way you'll know exactly what's goind bad.
One way to check for the error is creating a PHP file next to the image with:
<?php
echo imagecreatefromjpeg("path/to/image");
?>
run it in using 'php filename.php'
and looking in the console for the result
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an error while i try to activate a wp plugin:
Parse error: syntax error, unexpected '[' at this line :
$site_name = json_decode(curl_exec($ch), true)["site_name"];
This can be fixed by setting PHP to >= 5.4 OR with the post below.
You can only use that syntax in PHP >= 5.4. Otherwise, you need to save the result from json_decode into its own variable and then use that to get the site_name.
$JSONdata = json_decode(curl_exec($ch), true);
$site_name = $JSONdata["site_name"];
You need PHP 5.4 for this to work. If you are running older PHP version (and assuming this plugin does not need PHP5.4 for other things) then replace this code with:
$tmp = json_decode(curl_exec($ch), true);
$site_name = $tmp["site_name"];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to insert some values to my table, but i keep getting the following Notice :
Notice: Array to string conversion in C:\Program Files\wamp\www\process_invitation.php on line 10
Line 10 is the insertion line in the following code :
if ((isset($_POST['inviter'], $_POST['opponent'])) && ($_POST['inviter'] != '' && $_POST['opponent'] != '' )) {
$inviter = $_POST['inviter'];
$opponent = $_POST['opponent'];
$now = time();
if ($mysqli->query("INSERT INTO invitations (inviter_user_id, invited_user_id, time) VALUES ('$inviter','$opponent','$now')")) {
return;
}
}
Have you tried var_dumping $inviter and $opponent to see what datatype they are? Try just casting them to (string) too. Also, it's a good idea to use parameterised queries with mysqli. As it stands, you're leaving yourself wide open to SQL injections.
Print the output of variables & you will know where the problem is:
print_r($inviter);
print_r($opponent);
Make sure to extract value from the array before mysql insertion.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Improve this question
I'm getting this error
( ! ) Warning:
file_get_contents(wp-content/uploads/2013/06/wpid-Mt-ZionlaunchA4poster.jpg<br>)
[function.file-get-contents]:
failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in
C:\wamp\www\progjet\3\post.php on line 55
As you can see, there is a <br> tag at the end of the file name.
Here is my code:
foreach($imgf as $imgr){
echo $imgr;
$upimg[] = get_img(trim($imgr));
}
function get_img($url){
file_put_contents(basename($url),file_get_contents($url));
$path = 'http://'. $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']);
return $path.'/'. basename($url);
}
as you can see I add echo to see the what is going on
and here is the out put
What is the source of $imgf. If it's POST / GET data then you need to remove <br> from POST/GET request page.
If you want to remove the <br> using PHP you can do like this:
$imgr = str_replace('<br>', '', $imgr);