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 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题