編集: error_reporting(-1); を追加すると 次のエラー メッセージが表示されます。
Notice: Trying to get property of non-object in /data/22/2/145/126/2960289/user/3282682/htdocs/add.php on line 106
Notice: Undefined offset: 0 in /data/22/2/145/126/2960289/user/3282682/htdocs/add.php on line 108
Simple HTML DOM Parser (http://simplehtmldom.sourceforge.net/) を使用してサイトを検索しようとしています。localhost では問題なく動作しましたが、ホスト (ネットワーク ソリューション) にアップロードすると、一部の URL で結果が得られなくなりました。file_get_html から str_get_html に切り替えたときに発生しました。私はホストのためにそれをしなければなりませんでした。何が悪いのか分かりますか?スクリプトがどのように見えるかについては申し訳ありませんが、私は一種の初心者です...少し圧縮する方法についていくつかのヒントがありますか? スクリプトは次のようになります。
<?php
session_start();
include("connect.php");
if (isset($_POST['done'])) {
function getimg($url) {
$headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
$return = curl_exec($process);
curl_close($process);
return $return;
}
$imgurl = $_POST['finalimg'];
$random = substr(number_format(time() * rand(),0,'',''),0,30);
$imagename= basename($random.".jpg");
if(file_exists('./upload/'.$imagename)){continue;}
$image = getimg($imgurl);
file_put_contents('upload/'.$imagename,$image);
$finalurl = $_POST['finalurl'];
$description = $_POST['description'];
$titlen = $_POST['title'];
$pricen = $_POST['price'];
$sql = "INSERT INTO samples(description, name, productUrl, imageUrl, price)
VALUES('$description', '$titlen', '$finalurl', '$imagename', '$pricen')";
mysql_query($sql);
// Skickar vidare
header("Location: collection.php?id={$_SESSION['sess_id']}");
exit;
}
include("head.php");
?>
<div class="content add-content">
<div class="header">
<h1>Add your picture</span></h1>
</div>
<?php
if (isset($_POST['submit'])) {
require('DOM/simple_html_dom.php');
require('DOM/example/url_to_absolute.php');
$url = $_POST['url'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
$str = curl_exec($curl);
curl_close($curl);
$html = str_get_html($str);
foreach($html->find('img') as $element) {
$linktoimg = url_to_absolute($url, $element->src);
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $linktoimg);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
$new_image = ImageCreateFromString($contents);
imagejpeg($new_image, "temp.jpg",100);
$size = getimagesize("temp.jpg");
// width and height
$width = $size[0];
$height = $size[1];
echo $linktoimg . " Height: " . $height . " Width: " . $width . "<br />";
if ($height >= 200) {
$title1 = $html->find(".product_title");
$title11 = $title1[0]->plaintext;
$title2 = $html->find(".product-title");
$title22 = $title2[0]->plaintext;
$title3 = $html->find(".product_name");
$title33 = $title3[0]->plaintext;
$title4 = $html->find(".product_name");
$title44 = $title4[0]->plaintext;
$title5 = $html->find("h1");
$title55 = $title5[0]->plaintext;
if ($title55 != "") {
$title = $title55;
}
if ($title44 != "") {
$title = $title44;
}
if ($title33 != "") {
$title = $title33;
}
if ($title22 != "") {
$title = $title22;
}
if ($title11 != "") {
$title = $title11;
}
$desc1 = $html->find("p .product_description");
$desc11 = $desc1[0]->plaintext;
$desc2 = $html->find("p .product-description");
$desc22 = $desc2[0]->plaintext;
$desc3 = $html->find("p .description");
$desc33 = $desc3[0]->plaintext;
$desc4 = $html->find(".product-description");
$desc44 = $desc4[0]->plaintext;
$desc5 = $html->find(".product_description");
$desc55 = $desc5[0]->plaintext;
$desc6 = $html->find(".description");
$desc66 = $desc6[0]->plaintext;
if ($desc66 != "") {
$desc = $desc66;
}
if ($desc55 != "") {
$desc = $desc55;
}
if ($desc44 != "") {
$desc = $desc44;
}
if ($desc33 != "") {
$desc = $desc33;
}
if ($desc22 != "") {
$desc = $desc22;
}
if ($desc11 != "") {
$desc = $desc11;
}
$price1 = $html->find(".product_price");
$price11 = $price1[0]->plaintext;
$price2 = $html->find(".product-price");
$price22 = $price2[0]->plaintext;
$price3 = $html->find(".price");
$price33 = $price3[0]->plaintext;
$price4 = $html->find("#product_price");
$price44 = $price4[0]->plaintext;
$price5 = $html->find("#product-price");
$price55 = $price5[0]->plaintext;
$price6 = $html->find("#price");
$price66 = $price6[0]->plaintext;
$price7 = $html->find(".product_price_details");
$price77 = $price7[0]->plaintext;
$price8 = $html->find(".price-red");
$price88 = $price8[0]->plaintext;
if ($price88 != "") {
$price = $price88;
}
if ($price77 != "") {
$price = $price77;
}
if ($price66 != "") {
$price = $price66;
}
if ($price55 != "") {
$price = $price55;
}
if ($price44 != "") {
$price = $price44;
}
if ($price33 != "") {
$price = $price33;
}
if ($price22 != "") {
$price = $price22;
}
if ($price11 != "") {
$price = $price11;
}
?>
<form action="add.php" method="post">
<div class="add-wrapper">
<div style="font-weight: bold; font-size: 18px; margin-bottom: 20px; margin-top: -40px;">Is this a good picture?</div>
<div class="add-window">
<img src="timthumb.php?src=<?php echo $linktoimg; ?>&zc=3&h=432&w=500" style="border: 1px solid #000;" />
</div>
</div>
<input type="hidden" name="finalurl" value="<?php echo $_POST['url']; ?>">
<input type="hidden" name="finalimg" value="<?php echo $linktoimg; ?>">
<input type="hidden" name="title" value="<?php echo $title; ?>">
<input type="hidden" name="description" value="<?php echo $desc; ?>">
<input type="hidden" name="price" value="<?php echo $price; ?>">
<input type="submit" value="" class="submit" name="done" style="margin-top: 40px;">
</form>
<?php
echo $title . "<br /><br />";
echo $desc . "<br /><br />";
echo $price;
break;
}
}
} else {
?>
<form action="add.php" method="post">
<div class="add-url">
<input type="text" name="url" class="biginput" placeholder="Paste the link...">
<input type="submit" class="bigsubmit" value="" name="submit">
</div>
</form>
<?php
}
?>
</div>
<?php
include("feet.php");
?>