开发者社区> 问答> 正文

如何通过php的curl模拟ajax请求,获取其返回值? 400 报错

如何通过php的curl模拟ajax请求,获取其返回值? 400 报错

   如题,请问如何通过php的curl库获取ajax的返回值,例如有个ajax请求


$.ajax({
					type:"POST",
					url:"xxx.php",
					data:jQuery.param(data),
					success:function(msg){
					if(msg == "yes"){					 
						window.open('123.php');
					}
					if(msg == "no"){
						window.location.
					}
				}

现在需要通过curl获取 xxx.php的ajax服务地址,需要获取返回的msg。如何实现?


展开
收起
爱吃鱼的程序员 2020-05-29 19:58:09 574 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    有用过curl的同学快快现身 ######

    你的问题的重点不在怎么使用curl上,而是怎么从curl获取的内容中筛选你要的数据。

    如果是json,直接json_decode;如果是html/text,最直接的方法就是正则匹配。

    ######
    $str = array ( 'Auth'=>'admin', 'CustomerNumber'=>$customerNumber, 'Addtime'=>$addtime , 'City'=>$city, 'Suppliers'=>$suppliers, 'Domain'=>$domain );

    $url = "http://www.xxx.com/xxx.php"; $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_setopt ( $ch, CURLOPT_POST, 1 ); curl_setopt ( $ch, CURLOPT_HEADER, 0 ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $str ); $return = curl_exec ( $ch ); curl_close ( $ch );

    那边用$_POST接一下,大体就是这样哈



    2020-05-29 19:58:10
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
PHP安全开发:从白帽角度做安全 立即下载
复杂PHP系统性能瓶颈排查及优化 立即下载