dougu2006 2013-03-29 14:48
浏览 29
已采纳

检索另一个网站的内容

I want to get the content of another page. The background is that I wanted to make an AJAX request but due to the Same Origin Policy I cannot do this. Now I wanted to write an own PHP script on which I make the AJAX request. The URL looks like the following:

http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332

I tried it with fopen, curl and file_get_contents. Nothing from the works. The problem is if I put in the URL as string like

$results = file_get_contents('http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332');

it does work. If I put in a variable

$url = 'http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332';
$results = file_get_contents($url);

I come to a wrong page. With the specific parameter I get a result. If the parameter are not given correctly it seems that I come to a default page. I can't make a sense out of it.

The same for curl:

$curlSession = curl_init();
$options = array
(
    CURLOPT_URL=>$url,
    CURLOPT_HEADER=>false,
    CURLOPT_RETURNTRANSFER=>true,
    CURLOPT_FOLLOWLOCATION=>true
);
curl_setopt_array($curlSession,$options);
$results = curl_exec($curlSession);

This doesn't work. If I put in the URL as string and not as variable I get some results! I thought that the ampersand & or the square brackets [] are the problem but I cannot say this. & should be reserved and [] are no correct URL parameters. But why does the direct input work and not the variable?

I used the variable because I make some replacements with str_replace where I make the query more flexible.

I saw similar questions here (cURL function not working, curl_setopt doesnt work with url as a variable) but there was never posted a real solution.

  • 写回答

3条回答 默认 最新

查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口