bobdxcool
Posts: 84
Joined: Sun Apr 14, 2013 12:41 pm

Calling php script from python- http 500 error

Thu Jun 12, 2014 12:40 am

Hi guys

I am trying to call a php file (for huawei E3131) from the python script.

My python script is as follows:

Code: Select all

import urllib2
from urllib import urlencode
url = 'http://127.0.0.1:8080/getsms.php'
values = {'boxNo':'1'}
data = urlencode(values)
resp = urllib2.urlopen(url,data)
print resp.read()
The php file is as follows

Code: Select all

<?php
$boxNo = $_GET['boxNo'];
echo $boxNo;
getList($boxNo);

// Usage of Huwaei E3131 as SMS Gateway
// Request responses can be seen on: http://chaddyhv.wordpress.com/2012/08/13/programming-and-installing-huawei-hilink-e3131-under-linux/
// Setting up E3131 for Raspberry pi (Wheezy), see: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=36&t=18996
	function sendRequest($URL, $xml_data) {
		$ch = curl_init($URL);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
		curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, false);

		$output = curl_exec($ch);
		curl_close($ch);
		$xml = simplexml_load_string($output);
		return $xml;
	}
      
	function getList($boxNo, $pageNo = 1) {
		// 1 for inbox, 2 for outbox
		$URL = "http://192.168.1.1/api/sms/sms-list";
		$xml_data =	"<request>".
    				"<PageIndex>".$pageNo."</PageIndex>".
        			"<ReadCount>20</ReadCount>".
        			"<BoxType>".$boxNo."</BoxType>".
					"<SortType>0</SortType>".
        			"<Ascending>0</Ascending>".
					"<UnreadPreferred>0</UnreadPreferred>".
					"</request>";
		return sendRequest($URL, $xml_data)-> Messages;
	}

?>

192.168.1.1 is the URL of the USB modem connected to the pi. The USB modem is conencted properly to the pi

I am using instructions from these pages.

http://dtuunicef.wordpress.com/2013/05/ ... 3g-dongle/

The weird thing is that if I remove curl function from the first function in the php script, it runs without errors.

I get the following error
raceback (most recent call last):
File "/home/pi/Desktop/phptest.py", line 6, in <module>
resp = urllib2.urlopen(url)
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 407, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 520, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 445, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 528, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 500: Internal Server Error

riklaunim
Posts: 265
Joined: Tue Apr 22, 2014 7:34 pm

Re: Calling php script from python- http 500 error

Thu Jun 12, 2014 5:24 am

Odd problem. You could try calling the PHP file locally (php-cli) and it that does't help - reimplement it in Python.

Return to “Python”