drjtua5953 2010-11-25 21:30
浏览 273
已采纳

php curl - 通过浏览器响应访问url:通过curl响应访问url:401?

I have a url which contains all information in the url (username/password/content ect)

If i visit the url in my browser I get a successful response.

However If I visit the url through curl I get 401.

There is no authentication on the url.

What could be causing this?

Code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://mconnect.co.nz/v1/sendmessage?appname=app&pass=pass&msgclass=test&msgid=6&body=Some+Content&to=02712345678');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $mconnect[$index]['app_name'] . ":" . $mconnect[$index]['app_pass']);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.12 (KHTML, like Gecko) Chrome/9.0.587.0 Safari/534.12');
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_POST, false);
if(curl_exec($ch) === false) 
  echo 'fail: '.curl_error($ch);


Update...Strange...

So,

I am building my url through $mconnect[$index]['url'] . '?' . http_build_query($url);

which gives me the above url.

So,

If I have

$url = $mconnect[$index]['url'] . '?' . http_build_query($url);
curl_setopt($ch, CURLOPT_URL, $url);

I get the 401.

But,

If I then do echo $mconnect[$index]['url'] . '?' . http_build_query($url);

I get

http://mconnect.co.nz/v1/sendmessage?appname=app&pass=pass&msgclass=test&msgid=6&body=Some+Content&to=02712345678

so if I then do

$url = 'http://mconnect.co.nz/v1/sendmessage?appname=app&pass=pass&msgclass=test&msgid=6&body=Some+Content&to=02712345678';
    curl_setopt($ch, CURLOPT_URL, $url);

Then it works...

any ideas?

  • 写回答

5条回答

  • douhuangjian9627 2010-12-01 09:41
    关注

    Although not sure why The fix was to build the URL myself instead of using php 5's build_query_string

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64