如何使用cURL从网站获取特定数据,然后使用php将其保存到我的数据库中

问题描述:

谁能告诉我如何使用curl或file_get_contents从网站下载特定数据,然后将这些特定数据保存到我的mysql数据库中.我想从此网站 http://www.traileraddict.com/获取最新的电影,我想要将其保存在我的数据库中(每天;此文本和html链接将显示在我的网站上).我只需要文本和html链接.(在图片中突出显示)

can any one tell me how to use curl or file_get_contents for downloading specific data from a website and then save those specific data into my mysql database. I want to get latest additions of films from this website http://www.traileraddict.com/ and i want to save it in my database(on a daily basis; this text and html link will be shown on my website). I just need the text and html link.(highlighted in the pic)

我到处搜索过,但没有找到任何有用的教程.我有两个主要问题要问

i have searched everywhere but i didnt find any useful tutorial. i have two main questions to ask

1)如何使用cURL或file_get_contents获取特定数据.

1) How can i get specific data using cURL or file_get_contents.

2)如何将特定内容保存到我的mysql数据库表(一列中的文本,另一列中的链接)

2) How can i save the specific content to my mysql database table( text in one column and link in another column)

使用cURL:

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'http://www.something.com');
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);

$content = curl_exec($ch);

然后,您可以将元素加载到DOM对象中,并为特定数据解析dom.您也可以尝试使用搜索字符串来解析数据,但是在HTML上使用正则表达式的做法却不那么受欢迎.

Then you can load the element into a DOM Object and parse the dom for the specific data. You could also try and parse the data using search strings, but using regex on HTML is highly frowned upon.

$dom = new DOMDocument();
$dom->loadHTML( $content );

// Parse the dom for your desired content