帮忙看下 CURL 请求网页 api.php 的问题
本文介绍了帮忙看下 CURL 请求网页 api.php 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
我想用 curl 来得到一个网页 api.php 里面的数据,可总是得不到数据。请问我写的代码问题出在哪里
这个是他自己的网页调用 api.php 的代码
$.POST('api.php',{'url':''},function(data){
code...
},json);
这个是他网页调用时的 request header
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:50
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:Hm_lvt_XXXXX; a8167_pages=6; a8167_times=7
Host:XXX.cn
Origin:http://XXX.cn
Referer:http://XXX.cn/XXXX/?url=http://www.XXX.com/XXX.html
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
X-Requested-With:XMLHttpRequest
下面是我写的代码
<?php
$header[] = array('Content-Type: application/json','Content-Length: ' . strlen($postData));
$data= '{"url" : "http://www.XXX.com/XXX.html"}';
$refer = "http://XXX.cn/XXX/?url=http://www.XXX.com/XXX.html";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://XXX.cn/XXX/api.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $refer);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)");
$output = curl_exec($ch);
var_dump($output);
curl_close($ch);
?>
谢谢大家了。
解决方案
$data= 'url=http://www.XXX.com/XXX.html';
curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $header);注释这个
这篇关于帮忙看下 CURL 请求网页 api.php 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文