PHP Simple XML Parser, blank response.
The main problem with the code below is that without the parser it will work fine but as soon as the XML parser is added, so it can connect to the other server from the client, it will not return with the results, just a blank page?
Also this is a project, so before anyone ask's, why do you not have any SQLi protection. I have not as of yet implemented it yet.
Here is the code below:
Client side XML parser code: hinphp.php
<?php
$hin = $_GET["hin"];
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, "http://192.168.0.12/hinbuy.php?hin=");
curl_setopt($connection,CURLOPT_RETURNTRANSFER,1);
curl_setopt($connection,CURLOPT_HEADER, 0);
$response = curl_exec($connection);
$xml = simplexml_load_string($response);
for($index=0; $index < count($xml->songs); $index++)
{
echo $xml->songs[$index]->ID . "<br />";
echo $xml->songs[$index]->song . "<br />";
echo $xml->songs[$index]->artist . "<br />";
echo $xml->songs[$index]->year . "<br />";
echo $xml->songs[$index]->genre . "<br />";
echo $xml->songs[$index]->quantity . "<br />";
$ID = $xml->songs[$index]->ID;
echo "<a href='http://192.168.0.12/buyclihin.php?ID=$ID'>buy</a>";
}
curl_close($connection);
?>
Server side PHP select code: hinbuy.php
<?php
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='ISO-8859-1'?>";
echo "<hit>";
$hin = $_GET["hin"];
$conn = new PDO("mysql:host=localhost;dbname=xxxxx;","xxxxx","xxxxx");
$results = $conn->query("SELECT * FROM `ghit` WHERE `artist`='$hin'");
while($row=$results->fetch())
{
echo "<songs>";
echo "<id>$row[ID]</id>";
echo "<song>$row[song]</song>";
echo "<artist>$row[artist]</artist>";
echo "<year>$row[year]</year>";
echo "<genre>$row[genre]</genre>";
echo "<quantity>$row[quantity]</quantity>";
echo "</songs>";
}
echo "</hit>"
?>
Is it probably because you are not passing $hin to hibbuy.php?
Try:
$hin = $_GET["hin"];
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, "http://192.168.0.12/hinbuy.php?hin=" . $hin);
Related
i have try to use the webservice from some site,
i use nusoap library and my php code like this
<?php
require_once('lib/nusoap.php');
$client = new SoapClient("http://simlitabmas.dikti.go.id/ws_pimnas/ws.svc?wsdl");
$err = $client->getError();
if ($err) {
echo '<h2>errorr BOSSSS </h2><pre>' . $err . '</pre>';
}
$param = "examplekeyword";
$param2 = "001029";
$result = $client->call('get_peserta',array("keyword"=>"$param","kode_perguruan_tinggi"=>"$param2"));
if (!empty($result)) {
echo "<table border=1>";
echo "<tr bgcolor='#cccccc'>";
echo "<th>Name</th>";
echo "<th>Nomor Mahasiswa</th>";
echo "</tr>";
foreach ($result as $item) {
echo "<tr>";
echo "<td>".$item['nama']."</td>";
echo "<td>".$item['nomor_mahasiswa']."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
The function get_peserta has two parameter to input.
when i test my php code i got blank page. Any help?
Using the WSDL from http://simlitabmas.dikti.go.id/ws_pimnas/ws.svc?wsdl, you could generate the corresponding package from wsdltophp.com in order to be sure on how to structure your request in PHP as every element will be a PHP object with setters/getters. Let me know if you need any additional help,
Hi I am working on trying to get my app to load json files from a folder and decode to a database using the following code :
<?php
$con = mysqli_connect("localhost", "root", "", "json_map");
$response = array();
$res = array();
foreach(glob('C:\xampp\htdocs\laravel\awsconfig\app\JSON_Files') as $filename) {$json = file_get_contents($filename);
if ($json != null) {
$decoded = json_decode($json, true);
//$decode= var_dump($decoded);
//$ss=$decode["array"];
//echo $decoded['number'];
if (is_array($decoded["configurationItems"])) {
foreach ($decoded["configurationItems"] as $configurationItems)
//for($i=0;$i>sizeof($decoded["configurationItems"]);$i++)
{
$configurationItemVersion = $configurationItems["configurationItemVersion"];
echo "<br />", "configuration_Item_Version:", $configurationItemVersion, "<br />";
$configurationItemCaptureTime = $configurationItems["configurationItemCaptureTime"];
echo "configurationItemCaptureTime:", $configurationItemCaptureTime, "<br />";
$configurationStateId = $configurationItems["configurationStateId"];
echo "configurationStateId:", $configurationStateId, "<br />";
$awsAccountId = $configurationItems["awsAccountId"];
echo "awsAccountId:", $awsAccountId, "<br />";
$configurationItemStatus = $configurationItems["configurationItemStatus"];
echo "configurationItemStatus:", $configurationItemStatus, "<br />";
$resourceId = $configurationItems["resourceId"];
echo "resourceId:", $resourceId, "<br />";
$ARN = $configurationItems["ARN"];
echo "ARN:", $ARN, "<br />";
$awsRegion = $configurationItems["awsRegion"];
echo "awsRegion:", $awsRegion, "<br />";
$availabilityZone = $configurationItems["availabilityZone"];
echo "availabilityZone:", $availabilityZone, "<br />";
$configurationStateMd5Hash = $configurationItems["configurationStateMd5Hash"];
echo "configurationStateMd5Hash:", $configurationStateMd5Hash, "<br />";
$resourceType = $configurationItems["resourceType"];
echo "resourceType:", $resourceType, "<br />";
$resourceCreationTime = $configurationItems["resourceCreationTime"];
echo "resourceCreationTime:", $resourceCreationTime, "<br />";
$result = mysqli_query($con, "INSERT INTO configuration_item(configuration_item_version,configuration_item_capture_time,configuration_state_id, aws_account_id, configuration_item_status, resource_id, arn, aws_region, availability_zone,configuration_state_md5_hash, resource_type, resource_creation_time)
VALUES('$configurationItemVersion','$configurationItemCaptureTime','$configurationStateId','$awsAccountId','$configurationItemStatus','$resourceId','$ARN','$awsRegion','$availabilityZone','$configurationStateMd5Hash','$resourceType','$resourceCreationTime' )") or die("Insert Failed " . ((is_object($con)) ? mysqli_error($con) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
;
}
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["code"] = 1;
$response["message"] = "successfully stored configuration items ";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["code"] = 2;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
}
?>
I know it will be something stupid like a bracket but even using an IDE I am still no seeing it
Other than the missing foreach on line 7 (I'm assuming a mistake, because it would give a different syntax error message).
Your actual syntax error is a missing closing brace. Add } to the end of your code before the closing ?> and you have valid syntax. Whether you have working code is another matter. Try formatting your code with better indentation and you will more easily see where the missing tokens should be.
As a side note: A closing ?> is not required in PHP and I recommend against it.
I need to extract the thumbnail image from a video to facebook.
I should do it via json, the fact is that I can not extract the image to 720px.
I did so, but I get nothing, no errors or anything.
<?php
/*
10152765849330530
https://www.facebook.com/video.php?v=10152765849330530&set=vb.48166220529&type=2&theater
*/
$id = "10152765849330530";
$xml = #file_get_contents('https://graph.facebook.com/' . $id);
$result = #json_decode($xml);
//var_dump($result);
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
$result = $result->format->picture;
echo $result;
?>
Ideas?
The link is to be extracted:
https://fbcdn-vthumb-a.akamaihd.net/hvthumb-ak-xfp1/v/t15.0-10/10470126_10152765850455530_10152765849330530_61772_571_b.jpg?oh=ea81bafb0b88da98068fe8697b058bd0&oe=5493EF9F&gda=1422872671_cf3d63eef79db332df7a30c8041abdd9
Just tried to play with your code, works now -
<?php
/*
10152765849330530
https://www.facebook.com/video.php?v=10152765849330530&set=vb.48166220529&type=2&theater
*/
$id = "10152765849330530";
$xml = file_get_contents('http://graph.facebook.com/' . $id);
$result = json_decode($xml);
echo "<pre>";
echo $result->format[0]->picture;
?>
To fetch the 720px sized picture -
<?php
/*
10152765849330530
https://www.facebook.com/video.php?v=10152765849330530&set=vb.48166220529&type=2&theater
*/
$id = "10152765849330530";
$xml = file_get_contents('http://graph.facebook.com/' . $id);
$result = json_decode($xml);
echo "<pre>";
echo $result->format[3]->picture;
?>
I recently switched my code for accessing my database to a PHP PDO Object.
I have everything working accept for my ajax page. As far as I have been able to tell all the queries and data are being pulled out properly, however I get the following error when I try using PDO this was working before with a mysql_connect object.
I did find that if I comment out these lines it will run but then it is unable to run the query which causes more errors obviously.
//ini_set('include_path', 'C:\www\capc\libraries');
//include '/php/capc.php';
//include '/php/bio.php';
Error Message:
This page contains the following errors:
error on line 4 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.
CAPC Class query function
public function query($sql) {
try {
$handler = new PDO('mysql:host=' . $this->dbhost . ';dbname=capc', $this->dbuser, $this->dbpass);
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e->getMessage();
}
$query = $handler->prepare($sql);
$query->execute();
return $query;
}
Ajax.php
<?php
ini_set('include_path', 'C:\www\capc\libraries');
include '/php/capc.php';
include '/php/bio.php';
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
$bio_first = $_GET['First_Name'];
$bio_last = $_GET['Last_Name'];
if (!empty($bio_first) && !empty($bio_last)) {
$capc = new CAPC;
$sql = 'SELECT Bio_ID FROM `bio_users` WHERE Bio_First = "' . $bio_first . '" AND Bio_Last = "' . $bio_last . '";';
$query = $capc->query($sql);
$num_results = $query->rowCount();
}
echo '<bio>';
if ($num_results > 0) {
$bio_first = $capc->sanitize($bio_first, "string");
$bio_last = $capc->sanitize($bio_last, "string");
$bio = new Bio($bio_first, $bio_last);
echo '<bio_exists>';
echo 'True';
echo '</bio_exists>';
echo '<bio_fname>';
echo $bio->Bio_First;
echo '</bio_fname>';
echo '<bio_lname>';
echo $bio->Bio_Last;
echo '</bio_lname>';
echo '<bio_img>';
echo $bio->Bio_Img;
echo '</bio_img>';
} else {
echo '<bio_exists>';
echo 'False';
echo '</bio_exists>';
}
echo '</bio>';
?>
Finally got it working by moving the include for bio.php not sure why this works now and wouldn't before.
Working Ajax.php
<?php
ini_set('include_path', 'C:\www\capc\libraries');
include '/php/capc.php';
$bio_first = $_GET['First_Name'];
$bio_last = $_GET['Last_Name'];
if (!empty($bio_first) && !empty($bio_last)) {
$capc = new CAPC;
$sql = 'SELECT Bio_ID FROM `bio_users` WHERE Bio_First = "' . $bio_first . '" AND Bio_Last = "' . $bio_last . '";';
$query = $capc->query($sql);
$num_results = $capc->query($sql)->rowCount();
}
if ($num_results > 0) {
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<bio>';
$bio_first = $capc->sanitize($bio_first, "string");
$bio_last = $capc->sanitize($bio_last, "string");
include '/php/bio.php';
$bio = new Bio($bio_first, $bio_last);
echo '<bio_exists>';
echo 'True';
echo '</bio_exists>';
echo '<bio_fname>';
echo $bio->Bio_First;
echo '</bio_fname>';
echo '<bio_lname>';
echo $bio->Bio_Last;
echo '</bio_lname>';
echo '<bio_img>';
echo $bio->Bio_Img;
echo '</bio_img>';
} else {
echo '<bio_exists>';
echo 'False';
echo '</bio_exists>';
}
echo '</bio>';
?>
I have a PHP script with which I want to read servers from database and connect to them with cURL. Servers responds with results from sql query. The problem is that script after each respond from server displays number 1. The ouput looks like this:
Server 1: some results
1Server 2: some results
1Server 3: some results
1
Here is the code that reads servers from database and connects to them:
<?php
$mysql_id = mysql_connect('localhost', 'ms', 'pass');
mysql_select_db('servers', $mysql_id);
mysql_query("SET NAMES utf8");
$query = "SELECT * FROM svr";
$result = mysql_query($query);
$num = mysql_num_rows($result);
while ($data = mysql_fetch_assoc($result))
{
$server[] = $data;
}
mysql_close($mysql_id);
$i = 0;
while($i < $num) {
$dealer = $server[$i]['dealer'];
echo $dealer . "<br />";
$data = "val=a"; //just for testing
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/html; charset=utf-8')
);
$result = curl_exec($ch);
echo $result;
$i++;
}
?>
I discovered that 1 is displayed with "echo $result;" and the code for creating response is this:
<?php
$mysql_id1 = mysql_connect('localhost', 'ms', 'pass');
mysql_select_db('servers', $mysql_id1);
mysql_query("SET NAMES utf8");
$query2 = "SELECT * FROM data";
$result2 = mysql_query($query2);
$num2 = mysql_num_rows($result2);
while ($data2 = mysql_fetch_assoc($result2))
{
$deli[] = $data2;
}
$i1 = 0;
$space = " ";
while ($i1 < $num2) {
echo $space . $deli[$i1]['id'] . " ";
echo $deli[$i1]['artikel'] . " ";
echo $deli[$i1]['znamka'] . " ";
echo $deli[$i1]['model'] . " ";
echo $deli[$i1]['letnik'] . " ";
echo $deli[$i1]['cena'] . " € ";
echo $deli[$i1]['zaloga'] . "<br />";
$i1++;
}
echo "<br />";
mysql_close($mysql_id1);
?>
Please help me
Use the CURLOPT_RETURNTRANSFER option. Otherwise cURL will automatically echo the data and just return true (which is converted to 1 by echo).
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
PHP.net says,
TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.
You need to use CURLOPT_RETURNTRANSFE or curl_exec returns a statuscode and sends the response to stdout:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
CURLOPT_RETURNTRANSFER shoud be TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.read the full documentation php.net
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
or you can do
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Use CURLOPT_RETURNTRANSFE or else it will return a status-code and sends the response to stdout:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Man, if the user can change dealer field or $deli fields you have got stored XSS vulnerability here.
$dealer = $server[$i]['dealer'];
echo $dealer . "<br />";
echo $space . $deli[$i1]['id'] . " ";
...etc
use
htmlentities($your fields)
to solve this problem