ขอความช่วยเหลือเรื่อง file_get_contents เปลี่ยนมาเป็น curl หน่อยครับ -_-"

เริ่มโดย nipon91, 05 มกราคม 2012, 08:28:39

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

nipon91

ขอความช่วยเหลือเรื่อง file_get_contents เปลี่ยนมาเป็น curl หน่อยครับพยามศึกษาแล้ว แต่ว่าไม่มีความรู้ด้านนี้เลยครับ

เริ่มจากบอร์ดนี้ http://www.thaiseoboard.com/index.php?topic=251533.msg3467282;topicseen ผมเจอปัญหา Script ที่เจ้าของลอยแพพวกผมไปแล้ว  :-X

และตอนนี้เจอปัญหาเวลาที่ดึงข้อมูลสินค้าแบบนี้ครับ

ERROR 2: file_get_contents(http://www.amazon.com/review/product/B001D205XG): failed to open stream: HTTP request failed! HTTP/1.1 503 Service Temporarily Unavailable , Line 390 of /home/a8392hos/public_html/goodluxurywatches.co.cc/handlers/amazon.php

ซึ่งมีผู้ใจดีแนะนำว่า amazon เค้าเปลี่ยนมาเป็น curl แล้ว

ลองศึกษาดูในบอร์ดนี้ มีคนบอกว่าให้ใส่อันนี้เข้าไป


         function get_content($URL) {
         $ch = curl_init();
         $timeout = 0; // set to zero for no timeout
         $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
         curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
         curl_setopt ($ch, CURLOPT_URL, $URL);
         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
          $String = curl_exec($ch);
          curl_close($ch);
           return $String;
}


ลองแล้วแต่ก็ไม่รู้จะเอาไปใส่ตรงไหน เลยอยากให้ผู้รู้ช่วยหน่อยครับ พยายามแล้วไม่ได้จริง ๆ ครับ
อันนี้เป็นบรรทัดของ code ที่มีปัญหาครับ

}
function getreview($asin,$lang=false)
{
lz::h('db')->close();
$art=@file_get_contents('http://www.amazon.'.lz::$c['zone'].'/review/product/'.$asin,null,stream_context_create(array('http'=>array('ignore_errors'=>true,'method'=>"GET",'header'=>"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 GTB7.1 ( .NET CLR 3.5.30729)\r\n"))));
require_once(dirname(__FILE__).'/simple_html_dom.php');
$html=str_get_html($art);
if($c=$html->find('.swSprite',0))
{
$c=$c->parent;
$avg=false;
$tavg=false;
$review=false;
if(preg_match('/s\_star\_([0-9]+)\_([0-9]+)/',$c->find('span',0)->class,$d1))
{
$avg=$d1[1].'.'.$d1[2];
}

if(!$c=$html->find('.crAvgStars',0))
{
$c=$html->find('.swSprite',0);
$c=$c->parent;
if(preg_match('/([0-9,]+) customer review/',$c->find('a',0)->innertext,$d1))
{
$tavg=$d1[1];
}
}
else
{
if(preg_match('/([0-9,]+) customer review/',$c->find('a',1)->innertext,$d1))
{
$tavg=$d1[1];
}
}

$c=$html->find('#productReviews',0);
if($c)
{
$review=array();
$html=explode('<!-- BOUNDARY -->',$c->innertext);
for($i=1;$i<=count($html);$i++)
{
$c=str_get_html($html[$i]);
if($d=$c->find('div',0))
{
//echo 'sss';
$e=$d->find('div');
if(strpos($e[0]->innertext,'The manufacturer commented on the review below')>-1)
{
$start=2;
}
elseif(strpos($e[0]->innertext,'people found the following review helpful')>-1)
{
$start=1;
}
else
{
$start=0;
}
$l2=trim(strip_tags($e[$start]->find('span',0)->innertext));
preg_match('/^(.+) out of 5 stars/',$l2,$q);
$l2=trim(strip_tags($e[$start]->find('b',0)->innertext));
$t=trim(strip_tags($e[$start]->find('nobr',0)->innertext));
$l3=$e[$start+1]->find('a',0)->href;
preg_match('/http\:\/\/www\.amazon\.'.lz::$c['zone'].'\/gp\/pdp\/profile\/(\w+)\//',$l3,$w);
$l4=strip_tags($e[$start+1]->find('a',0)->innertext);
for($j=0;$j<count($e);$j++)
{
$e[$j]->outertext='';
}
$txt=trim(strip_tags($d->innertext));
if(intval($q[1])>=3)
{
$review[]=array(
  'asin'=>$asin,
  'rating'=>intval($q[1]),
  'customerid'=>$w[1],
  'name'=>$l4,
  'date'=>date('Y-m-d',strtotime($t)),
  'summary'=>$l2,
  'content'=>nl2br($txt)
  );
}
}
}
shuffle($review);
$review=array_slice($review,0,3);
if($lang)
{
for($k=0;$k<count($review);$k++)
{
$tmp='';
if(lz::h('spinner')->connect())
{
$tmp=lz::h('spinner')->rewrite(strip_tags($review[$k]['content']));
}
if(!$tmp && $lang)
{
$tmp=lz::h('rewrite')->gen($review[$k]['content'],$lang);
}
if($tmp)
{
$review[$k]['content']=$tmp;
}
}
}
}
return array($avg,$tavg,$review);
}
}
}
?>


ขอบคุณครับ  :P

Queue

บรรทัดนี้
อ้างถึงfile_get_contents('http://www.amazon.com/review/product/B001D205XG')
เปลี่ยนเป็น
อ้างถึงget_content('http://www.amazon.com/review/product/B001D205XG')

ส่วนตัวนี้
อ้างถึงfunction get_content($URL) {
         $ch = curl_init();
         $timeout = 0; // set to zero for no timeout
         $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
         curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
         curl_setopt ($ch, CURLOPT_URL, $URL);
         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
          $String = curl_exec($ch);
          curl_close($ch);
           return $String;
}

ก็เอาไปแทรกๆ ไว้ตรงไหนใน page ก็ได้ จะสร้างเป็นไฟล์ แล้ว include เข้ามาก็สะดวก ดีนะครับ


nipon91

อ้างถึงจาก: Queue ใน 05 มกราคม 2012, 08:37:41
บรรทัดนี้
อ้างถึงfile_get_contents('http://www.amazon.com/review/product/B001D205XG')
เปลี่ยนเป็น
อ้างถึงget_content('http://www.amazon.com/review/product/B001D205XG')

ส่วนตัวนี้
อ้างถึงfunction get_content($URL) {
         $ch = curl_init();
         $timeout = 0; // set to zero for no timeout
         $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
         curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
         curl_setopt ($ch, CURLOPT_URL, $URL);
         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
          $String = curl_exec($ch);
          curl_close($ch);
           return $String;
}

ก็เอาไปแทรกๆ ไว้ตรงไหนใน page ก็ได้ จะสร้างเป็นไฟล์ แล้ว include เข้ามาก็สะดวก ดีนะครับ



ขอบคุณครับ +1
ยังไงแล้วผมจะลองดูนะครับ ได้ผลยังไงจะรายงานให้ทราบอีกครั้งครับ  :wanwan017:

nipon91

อ้างถึงจาก: Queue ใน 05 มกราคม 2012, 08:37:41
บรรทัดนี้
อ้างถึงfile_get_contents('http://www.amazon.com/review/product/B001D205XG')
เปลี่ยนเป็น
อ้างถึงget_content('http://www.amazon.com/review/product/B001D205XG')

ส่วนตัวนี้
อ้างถึงfunction get_content($URL) {
         $ch = curl_init();
         $timeout = 0; // set to zero for no timeout
         $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
         curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
         curl_setopt ($ch, CURLOPT_URL, $URL);
         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
          $String = curl_exec($ch);
          curl_close($ch);
           return $String;
}

ก็เอาไปแทรกๆ ไว้ตรงไหนใน page ก็ได้ จะสร้างเป็นไฟล์ แล้ว include เข้ามาก็สะดวก ดีนะครับ



ลองแล้วมันไม่ขึ้น error แล้วครับ แต่ว่าเวลาดึงสินค้ามันค้างอยู่อย่างนั้น เหมือนกับพันพยายามดึงสินค้าชิ้นแรก แล้วก็นิ่งเลยอ่ะครับดึงต่อไม่ได้
อันนี้ผมต้องแก้ยังไงต่อเหรอครับ  :P



nipon91


marus

แก้ให้แล้ว ลองเอาไปใช้ดู copy มาอย่างไร เอาอันนีัไปวางแทนอย่างนั้น

}
         function get_content($URL) {
         $ch = curl_init();
         $timeout = 0; // set to zero for no timeout
         $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
         curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
         curl_setopt ($ch, CURLOPT_URL, $URL);
         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
          $String = curl_exec($ch);
          curl_close($ch);
           return $String;
}
function getreview($asin,$lang=false)
{
lz::h('db')->close();
$art=@get_content('http://www.amazon.'.lz::$c['zone'].'/review/product/'.$asin,null,stream_context_create(array('http'=>array('ignore_errors'=>true,'method'=>"GET",'header'=>"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 GTB7.1 ( .NET CLR 3.5.30729)\r\n"))));
require_once(dirname(__FILE__).'/simple_html_dom.php');
$html=str_get_html($art);
if($c=$html->find('.swSprite',0))
{
$c=$c->parent;
$avg=false;
$tavg=false;
$review=false;
if(preg_match('/s\_star\_([0-9]+)\_([0-9]+)/',$c->find('span',0)->class,$d1))
{
$avg=$d1[1].'.'.$d1[2];
}

if(!$c=$html->find('.crAvgStars',0))
{
$c=$html->find('.swSprite',0);
$c=$c->parent;
if(preg_match('/([0-9,]+) customer review/',$c->find('a',0)->innertext,$d1))
{
$tavg=$d1[1];
}
}
else
{
if(preg_match('/([0-9,]+) customer review/',$c->find('a',1)->innertext,$d1))
{
$tavg=$d1[1];
}
}

$c=$html->find('#productReviews',0);
if($c)
{
$review=array();
$html=explode('<!-- BOUNDARY -->',$c->innertext);
for($i=1;$i<=count($html);$i++)
{
$c=str_get_html($html[$i]);
if($d=$c->find('div',0))
{
//echo 'sss';
$e=$d->find('div');
if(strpos($e[0]->innertext,'The manufacturer commented on the review below')>-1)
{
$start=2;
}
elseif(strpos($e[0]->innertext,'people found the following review helpful')>-1)
{
$start=1;
}
else
{
$start=0;
}
$l2=trim(strip_tags($e[$start]->find('span',0)->innertext));
preg_match('/^(.+) out of 5 stars/',$l2,$q);
$l2=trim(strip_tags($e[$start]->find('b',0)->innertext));
$t=trim(strip_tags($e[$start]->find('nobr',0)->innertext));
$l3=$e[$start+1]->find('a',0)->href;
preg_match('/http\:\/\/www\.amazon\.'.lz::$c['zone'].'\/gp\/pdp\/profile\/(\w+)\//',$l3,$w);
$l4=strip_tags($e[$start+1]->find('a',0)->innertext);
for($j=0;$j<count($e);$j++)
{
$e[$j]->outertext='';
}
$txt=trim(strip_tags($d->innertext));
if(intval($q[1])>=3)
{
$review[]=array(
  'asin'=>$asin,
  'rating'=>intval($q[1]),
  'customerid'=>$w[1],
  'name'=>$l4,
  'date'=>date('Y-m-d',strtotime($t)),
  'summary'=>$l2,
  'content'=>nl2br($txt)
  );
}
}
}
shuffle($review);
$review=array_slice($review,0,3);
if($lang)
{
for($k=0;$k<count($review);$k++)
{
$tmp='';
if(lz::h('spinner')->connect())
{
$tmp=lz::h('spinner')->rewrite(strip_tags($review[$k]['content']));
}
if(!$tmp && $lang)
{
$tmp=lz::h('rewrite')->gen($review[$k]['content'],$lang);
}
if($tmp)
{
$review[$k]['content']=$tmp;
}
}
}
}
return array($avg,$tavg,$review);
}
}
}
?>


nipon91

อ้างถึงจาก: marus ใน 05 มกราคม 2012, 10:57:41
แก้ให้แล้ว ลองเอาไปใช้ดู copy มาอย่างไร เอาอันนีัไปวางแทนอย่างนั้น

}
         function get_content($URL) {
         $ch = curl_init();
         $timeout = 0; // set to zero for no timeout
         $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
         curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
         curl_setopt ($ch, CURLOPT_URL, $URL);
         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
          $String = curl_exec($ch);
          curl_close($ch);
           return $String;
}
function getreview($asin,$lang=false)
{
lz::h('db')->close();
$art=@get_content('http://www.amazon.'.lz::$c['zone'].'/review/product/'.$asin,null,stream_context_create(array('http'=>array('ignore_errors'=>true,'method'=>"GET",'header'=>"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 GTB7.1 ( .NET CLR 3.5.30729)\r\n"))));
require_once(dirname(__FILE__).'/simple_html_dom.php');
$html=str_get_html($art);
if($c=$html->find('.swSprite',0))
{
$c=$c->parent;
$avg=false;
$tavg=false;
$review=false;
if(preg_match('/s\_star\_([0-9]+)\_([0-9]+)/',$c->find('span',0)->class,$d1))
{
$avg=$d1[1].'.'.$d1[2];
}

if(!$c=$html->find('.crAvgStars',0))
{
$c=$html->find('.swSprite',0);
$c=$c->parent;
if(preg_match('/([0-9,]+) customer review/',$c->find('a',0)->innertext,$d1))
{
$tavg=$d1[1];
}
}
else
{
if(preg_match('/([0-9,]+) customer review/',$c->find('a',1)->innertext,$d1))
{
$tavg=$d1[1];
}
}

$c=$html->find('#productReviews',0);
if($c)
{
$review=array();
$html=explode('<!-- BOUNDARY -->',$c->innertext);
for($i=1;$i<=count($html);$i++)
{
$c=str_get_html($html[$i]);
if($d=$c->find('div',0))
{
//echo 'sss';
$e=$d->find('div');
if(strpos($e[0]->innertext,'The manufacturer commented on the review below')>-1)
{
$start=2;
}
elseif(strpos($e[0]->innertext,'people found the following review helpful')>-1)
{
$start=1;
}
else
{
$start=0;
}
$l2=trim(strip_tags($e[$start]->find('span',0)->innertext));
preg_match('/^(.+) out of 5 stars/',$l2,$q);
$l2=trim(strip_tags($e[$start]->find('b',0)->innertext));
$t=trim(strip_tags($e[$start]->find('nobr',0)->innertext));
$l3=$e[$start+1]->find('a',0)->href;
preg_match('/http\:\/\/www\.amazon\.'.lz::$c['zone'].'\/gp\/pdp\/profile\/(\w+)\//',$l3,$w);
$l4=strip_tags($e[$start+1]->find('a',0)->innertext);
for($j=0;$j<count($e);$j++)
{
$e[$j]->outertext='';
}
$txt=trim(strip_tags($d->innertext));
if(intval($q[1])>=3)
{
$review[]=array(
  'asin'=>$asin,
  'rating'=>intval($q[1]),
  'customerid'=>$w[1],
  'name'=>$l4,
  'date'=>date('Y-m-d',strtotime($t)),
  'summary'=>$l2,
  'content'=>nl2br($txt)
  );
}
}
}
shuffle($review);
$review=array_slice($review,0,3);
if($lang)
{
for($k=0;$k<count($review);$k++)
{
$tmp='';
if(lz::h('spinner')->connect())
{
$tmp=lz::h('spinner')->rewrite(strip_tags($review[$k]['content']));
}
if(!$tmp && $lang)
{
$tmp=lz::h('rewrite')->gen($review[$k]['content'],$lang);
}
if($tmp)
{
$review[$k]['content']=$tmp;
}
}
}
}
return array($avg,$tavg,$review);
}
}
}
?>



ขอบคุณครับ +1  :wanwan017:

ผมลองแล้วครับ แต่ผลที่คือ..ที่โฮสของ gator ใช้ได้ไม่มีปัญหาครับ
แต่กับของ hostable กลับค้างอยู่อย่างนั้นดึงสินค้าไม่ได้ อันนี้เกี่ยวกับโฮสใช่หรือเปล่าครับ  :P


marus


<?php
echo phpinfo();
?>



ลองเอา code ข้างบนไป save เป็นนามสกุล .php ดูเช่น phpinfo.php
แล้วลองเรียกดู แล้วหาคำว่า curl ดูว่าได้เปิดไว้ไหม
ถ้าไม่มีแปลว่าใช้ไม่ได้ไม่ได้เปิด curl ไว้
ถ้ามีอาจจะโดน limit แบนวิท

nipon91

อ้างถึงจาก: marus ใน 05 มกราคม 2012, 14:50:26

<?php
echo phpinfo();
?>



ลองเอา code ข้างบนไป save เป็นนามสกุล .php ดูเช่น phpinfo.php
แล้วลองเรียกดู แล้วหาคำว่า curl ดูว่าได้เปิดไว้ไหม
ถ้าไม่มีแปลว่าใช้ไม่ได้ไม่ได้เปิด curl ไว้
ถ้ามีอาจจะโดน limit แบนวิท

ลองดูแล้วผลออกมาแบบนี้อ่ะครับ

Build Date May 5 2011 13:52:55
Configure Command './configure' '--enable-bcmath' '--enable-calendar' '--enable-exif' '--enable-fastcgi' '--enable-force-cgi-redirect' '--enable-ftp' '--enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes' '--enable-mbstring' '--enable-pdo=shared' '--enable-sockets' '--enable-zip' '--prefix=/usr' '--with-bz2' '--with-config-file-path=/usr/local/lib' '--with-config-file-scan-dir=/usr/local/lib/php.ini.d' '--with-curl=/opt/curlssl/' '--with-freetype-dir=/usr' '--with-gd' '--with-gettext' '--with-imap=/opt/php_with_imap_client/' '--with-imap-ssl=/usr' '--with-jpeg-dir=/usr' '--with-kerberos' '--with-libxml-dir=/opt/xml2/' '--with-mcrypt=/opt/libmcrypt/' '--with-mysql=/usr' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-mysqli=/usr/bin/mysql_config' '--with-openssl=/usr' '--with-openssl-dir=/usr' '--with-pcre-regex=/opt/pcre' '--with-pdo-mysql=shared' '--with-pdo-sqlite=shared' '--with-png-dir=/usr' '--with-sqlite=shared' '--with-ttf' '--with-xpm-dir=/usr' '--with-zlib' '--with-zlib-dir=/usr'
Server API CGI/FastCGI
Virtual Directory Support disabled
Configuration File (php.ini) Path /usr/local/lib
Loaded Configuration File /usr/local/lib/php.ini
Scan this dir for additional .ini files /usr/local/lib/php.ini.d
additional .ini files parsed (none)


ดูแล้วมีคำว่า curl ด้วยครับ

Lufy W23

อ้างถึงแต่กับของ hostable กลับค้างอยู่อย่างนั้นดึงสินค้าไม่ได้ อันนี้เกี่ยวกับโฮสใช่หรือเปล่าครับ :P
hostable มันเออเร่อครับ ใช้ไม่ได้ ผมเองก็เป็น :P

nipon91

อ้างถึงจาก: Lufy W23 ใน 05 มกราคม 2012, 15:54:00
อ้างถึงแต่กับของ hostable กลับค้างอยู่อย่างนั้นดึงสินค้าไม่ได้ อันนี้เกี่ยวกับโฮสใช่หรือเปล่าครับ :P
hostable มันเออเร่อครับ ใช้ไม่ได้ ผมเองก็เป็น :P


ขอบคุณครับ ถึงบางอ้อเลย... :P