search
HomeBackend DevelopmentPHP ProblemWhat to do if php collects article pictures but does not display them

Solutions to the problem that php collected article images are not displayed: 1. Collect Baidu search results based on keywords; 2. Convert the collected HTML in batches based on the defined domain name.

What to do if php collects article pictures but does not display them

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

What should I do if the pictures collected by php are not displayed? ?

Solution to the problem that PHP CURL collects Baidu search result images and does not display them

1. Collect Baidu search results based on keywords

Based on Keyword collection Baidu search results can be implemented using curl. The code is as follows:

<p><?php <br/>function doCurl($url, $data=array(), $header=array(), $timeout=30){<br>  $ch = curl_init();<br>  curl_setopt($ch, CURLOPT_URL, $url);<br>  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);<br>  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br>  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);<br>  curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);<br>  $response = curl_exec($ch);<br>  if($error=curl_error($ch)){<br>    die($error);<br>  }<br>  curl_close($ch);<br>  return $response;<br>}<br>$wd = '仙剑奇侠传';<br>$url = 'http://www.baidu.com/s?wd='.urlencode($wd);<br>$data = array();<br>$header = array();<br>$response = doCurl($url, $data, $header, 5);<br>echo $response;<br>?><br></p>

After output, it is found that some pictures cannot be displayed

What to do if php collects article pictures but does not display them

2. After collection Analysis of the reasons why pictures are not displayed

Search directly in Baidu, and the page can display pictures. Use firebug to check the image path and find that the domain name of the collected image is different from the domain name of the image searched on Baidu.

Collected returned image domain name t11.baidu.com

What to do if php collects article pictures but does not display them

Normal search image domain name ss1.baidu.com

What to do if php collects article pictures but does not display them

Check the collected and normal search html, and found that the js for domain name conversion is different

Collection

<p>var list = {<br>    "graph.baidu.com": "http://graph.baidu.com",<br>    "t1.baidu.com":"http://t1.baidu.com",<br>    "t2.baidu.com":"http://t2.baidu.com",<br>    "t3.baidu.com":"http://t3.baidu.com",<br>    "t10.baidu.com":"http://t10.baidu.com",<br>    "t11.baidu.com":"http://t11.baidu.com",<br>    "t12.baidu.com":"http://t12.baidu.com",<br>    "i7.baidu.com":"http://i7.baidu.com",<br>    "i8.baidu.com":"http://i8.baidu.com",<br>    "i9.baidu.com":"http://i9.baidu.com",<br>};<br></p>

Normal search

<p>var list = {<br>    "graph.baidu.com": "https://sp0.baidu.com/-aYHfD0a2gU2pMbgoY3K",<br>    "t1.baidu.com":"https://ss0.baidu.com/6ON1bjeh1BF3odCf",<br>    "t2.baidu.com":"https://ss1.baidu.com/6OZ1bjeh1BF3odCf",<br>    "t3.baidu.com":"https://ss2.baidu.com/6OV1bjeh1BF3odCf",<br>    "t10.baidu.com":"https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq",<br>    "t11.baidu.com":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq",<br>    "t12.baidu.com":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq",<br>    "i7.baidu.com":"https://ss0.baidu.com/73F1bjeh1BF3odCf",<br>    "i8.baidu.com":"https://ss0.baidu.com/73x1bjeh1BF3odCf",<br>    "i9.baidu.com":"https://ss0.baidu.com/73t1bjeh1BF3odCf",<br>};<br></p>

So it can The conclusion is that Baidu determines if it is collected based on the source address, IP, header and other parameters, and returns different js.

3. Solution to the problem that images are not displayed after collection

Just do a batch conversion of the collected HTML according to the defined domain name.

<p><?php <br/>function doCurl($url, $data=array(), $header=array(), $timeout=30){<br>  $ch = curl_init();<br>  curl_setopt($ch, CURLOPT_URL, $url);<br>  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);<br>  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br>  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);<br>  curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);<br>  $response = curl_exec($ch);<br>  if($error=curl_error($ch)){<br>    die($error);<br>  }<br>  curl_close($ch);<br>  return $response;<br>}<br>// 域名转换<br>function cdomain($str){<br>  $baidu_domain = '{<br>    "http://graph.baidu.com": "https://sp0.baidu.com/-aYHfD0a2gU2pMbgoY3K",<br>    "http://p.qiao.baidu.com":"https://sp0.baidu.com/5PoXdTebKgQFm2e88IuM_a",<br>    "http://vse.baidu.com":"https://sp3.baidu.com/6qUDsjip0QIZ8tyhnq",<br>    "http://hdpreload.baidu.com":"https://sp3.baidu.com/7LAWfjuc_wUI8t7jm9iCKT-xh_",<br>    "http://lcr.open.baidu.com":"https://sp2.baidu.com/8LUYsjW91Qh3otqbppnN2DJv",<br>    "http://kankan.baidu.com":"https://sp3.baidu.com/7bM1dzeaKgQFm2e88IuM_a",<br>    "http://xapp.baidu.com":"https://sp2.baidu.com/yLMWfHSm2Q5IlBGlnYG",<br>    "http://dr.dh.baidu.com":"https://sp0.baidu.com/-KZ1aD0a2gU2pMbgoY3K",<br>    "http://xiaodu.baidu.com":"https://sp0.baidu.com/yLsHczq6KgQFm2e88IuM_a",<br>    "http://sensearch.baidu.com":"https://sp1.baidu.com/5b11fzupBgM18t7jm9iCKT-xh_",<br>    "http://s1.bdstatic.com":"https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K",<br>    "http://olime.baidu.com":"https://sp0.baidu.com/8bg4cTva2gU2pMbgoY3K",<br>    "http://app.baidu.com":"https://sp2.baidu.com/9_QWsjip0QIZ8tyhnq",<br>    "http://i.baidu.com":"https://sp0.baidu.com/74oIbT3kAMgDnd_",<br>    "http://c.baidu.com":"https://sp0.baidu.com/9foIbT3kAMgDnd_",<br>    "http://sclick.baidu.com":"https://sp0.baidu.com/5bU_dTmfKgQFm2e88IuM_a",<br>    "http://nsclick.baidu.com":"https://sp1.baidu.com/8qUJcD3n0sgCo2Kml5_Y_D3",<br>    "http://sestat.baidu.com":"https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a",<br>    "http://eclick.baidu.com":"https://sp3.baidu.com/-0U_dTmfKgQFm2e88IuM_a",<br>    "http://api.map.baidu.com":"https://sp2.baidu.com/9_Q4sjOpB1gCo2Kml5_Y_D3",<br>    "http://ecma.bdimg.com":"https://ss1.bdstatic.com/-0U0bXSm1A5BphGlnYG",<br>    "http://ecmb.bdimg.com":"https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG",<br>    "http://t1.baidu.com":"https://ss0.baidu.com/6ON1bjeh1BF3odCf",<br>    "http://t2.baidu.com":"https://ss1.baidu.com/6OZ1bjeh1BF3odCf",<br>    "http://t3.baidu.com":"https://ss2.baidu.com/6OV1bjeh1BF3odCf",<br>    "http://t10.baidu.com":"https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq",<br>    "http://t11.baidu.com":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq",<br>    "http://t12.baidu.com":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq",<br>    "http://i7.baidu.com":"https://ss0.baidu.com/73F1bjeh1BF3odCf",<br>    "http://i8.baidu.com":"https://ss0.baidu.com/73x1bjeh1BF3odCf",<br>    "http://i9.baidu.com":"https://ss0.baidu.com/73t1bjeh1BF3odCf",<br>    "http://b1.bdstatic.com":"https://ss0.bdstatic.com/9uN1bjq8AAUYm2zgoY3K",<br>    "http://ss.bdimg.com":"https://ss1.bdstatic.com/5aV1bjqh_Q23odCf",<br>    "http://opendata.baidu.com":"https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv",<br>    "http://api.open.baidu.com":"https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv",<br>    "http://tag.baidu.com":"https://sp1.baidu.com/6LMFsjip0QIZ8tyhnq",<br>    "http://f3.baidu.com":"https://sp2.baidu.com/-uV1bjeh1BF3odCf",<br>    "http://s.share.baidu.com":"https://sp0.baidu.com/5foZdDe71MgCo2Kml5_Y_D3",  <br>    "http://bdimg.share.baidu.com":"https://ss1.baidu.com/9rA4cT8aBw9FktbgoI7O1ygwehsv",<br>    "http://1.su.bdimg.com":"https://ss0.bdstatic.com/k4oZeXSm1A5BphGlnYG",<br>    "http://2.su.bdimg.com":"https://ss1.bdstatic.com/kvoZeXSm1A5BphGlnYG",<br>    "http://3.su.bdimg.com":"https://ss2.bdstatic.com/kfoZeXSm1A5BphGlnYG",<br>    "http://4.su.bdimg.com":"https://ss3.bdstatic.com/lPoZeXSm1A5BphGlnYG",<br>    "http://5.su.bdimg.com":"https://ss0.bdstatic.com/l4oZeXSm1A5BphGlnYG",<br>    "http://6.su.bdimg.com":"https://ss1.bdstatic.com/lvoZeXSm1A5BphGlnYG",<br>    "http://7.su.bdimg.com":"https://ss2.bdstatic.com/lfoZeXSm1A5BphGlnYG",<br>    "http://8.su.bdimg.com":"https://ss3.bdstatic.com/iPoZeXSm1A5BphGlnYG"<br>  }';<br>  $domain = json_decode($baidu_domain, true);<br>  foreach($domain as $k=>$v){<br>    $str = str_replace($k, $v, $str);<br>  }<br>  return $str;<br>}<br>$wd = '仙剑奇侠传';<br>$url = 'http://www.baidu.com/s?wd='.urlencode($wd);<br>$data = array();<br>$header = array();<br>$response = doCurl($url, $data, $header, 5);<br>echo cdomain($response); // 调用域名转换<br>?><br></p>

After adding domain name conversion, all pictures can be displayed normally.

What to do if php collects article pictures but does not display them

Recommended study: "PHP Video Tutorial"

The above is the detailed content of What to do if php collects article pictures but does not display them. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools