I have a php app which posts variable info to a flask app (which does some calculations and returns a result ). I'm running both locally on win7
When I test the url "127.0.0.1:5000/index" using a post with postman, I get a 200 status code (screenshot). However when the php app posts to the flask app I get:
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
I'm using CURL , and the verbose output is:
* About to connect() to 127.0.0.1 port 5000 (#0)
* Trying 127.0.0.1...
* connected
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /index/ HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)
Host: 127.0.0.1:5000
Accept: */*
Content-Length: 338
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------d5cb02e2edea
< HTTP/1.1 100 Continue
* HTTP 1.0, assume close after body
< HTTP/1.0 404 NOT FOUND
< Content-Type: text/html
< Content-Length: 233
< Server: Werkzeug/0.10.4 Python/2.7.5
My php code looks like:
$data= array('a'=>$a, 'token'=>$token);
$url="http://127.0.0.1:5000/index/";
$output = $this->my_model->get_data($url, $data);
public function get_data($url,$postFieldArray=FALSE) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
if ($postFieldArray!= FALSE) {
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFieldArray); //for django
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $verbose);
curl_setopt($ch, CURLOPT_URL, $url);
$html = curl_exec($ch);
curl_close($ch);
.......
return $result;
}
Simplified Flask app:
app = Flask(__name__)
app.debug = True
#app.route('/')
def hello_world():
return 'Hello World!'
#app.route('/index',methods=['POST'])
def index():
token = request.form['token']
a = request.form['a']
......
return
if __name__ == '__main__':
app.run()
What am I doing wrong?
You have a trailing slash in the $url variable in the PHP code. That won't work, because you don't have a trailing slash in your Flask code. Look here for more info, under the section "Unique URLs / Redirection Behavior"
Related
I have this situation:
I want to make a PUT request using curl to a form but it keeps retrieving this error: "send failure: connection aborted". Here is the function I use:
function PutCurl($url,$parametros_post){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch,CURLOPT_HEADER,false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_COOKIEJAR,'cookies.txt');
curl_setopt($ch,CURLOPT_COOKIEFILE,'cookies.txt');
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_PUT,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: '.strlen($parametros_post)));
curl_setopt($ch,CURLOPT_POSTFIELDS,$parametros_post);
$result = curl_exec($ch);
$rerror = curl_error($ch);
curl_close($ch);
if ($rerror != ""){
echo '<h3>'.$rerror.'</h3>';
return false;
}
else
return $result;
}
I get this error:
While trying to process the request:
PUT /textos-del-anuncio/ HTTP/1.0
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
Host: www.somesite.com
Pragma: no-cache
Accept: /
Cookie: PHPSESSID=gkmb7ksi1o82d91rino35ihma3
Expect: 100-continue
Connection: Close
X-Forwarded-For: Ip
Via: 1.0 Proxy+ (v4.00 http://www.proxyplus.cz)
Proxy-Authorization: Basic a2F2OjNHd3BiK3J3QiE=
The following error was encountered:
* Invalid Request
Some aspect of the HTTP Request is invalid. Possible problems:
* Missing or unknown request method
* Missing URL
* Missing HTTP Identifier (HTTP/1.0)
* Request is too large
* Content-Length missing for POST or PUT requests
* Illegal character in hostname; underscores are not allowed
Then I tried to set Content-Length in the header adding this line to my function:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: '.strlen($parametros_post)));
but then I get a "Send failure: Connection was aborted" error.
Please, someone who can help me. Thanks.
Something being wrong with the request, have you tried doing it like this:
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
So you do not use curl_setopt($ch,CURLOPT_PUT,true); but CURLOPT_CUSTOMREQUEST instead.
I assume your $parametros_post is an associative array like $parametros_post = array('post_field_name' => 'post_field_value');
I am opening a HTTPS page using cURL. The page I request issues a redirect request. I have set cURL to follow the redirect, but I cannot seem to be able to get it to request the correct page. I have tracked the same request in a browser and I see my browser making a different request to what cURL makes. What can I do to correct this? The correct URL is shown in the output of a verbose cURL dump. It follows the "* Issue another request to this URL"
Here is a snippet of the output from cURL's verbose output:
< HTTP/1.1 302 Moved Temporarily
< Location: /XXX
< Content-Type: text/html; charset=UTF-8
< Date: Tue, 31 Dec 2013 15:51:46 GMT
< Expires: Tue, 31 Dec 2013 15:51:46 GMT
< Cache-Control: private, max-age=0
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< Server: GSE
< Alternate-Protocol: 443:quic
< Transfer-Encoding: chunked
<
* Ignoring the response-body
* Connection #0 to host 127.0.0.1 left intact
* Issue another request to this URL: 'XYYYZ'
* Re-using existing connection! (#0) with host 127.0.0.1
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> GET /??? HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0
The PHP code I use follows:
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
$target = ADDR;
curl_setopt($ch, CURLOPT_URL, $target);
$page = curl_exec($ch);
cURL follows the Location: Header, but be sure to send the exact headers (content-language, referer) browser does using CURLOPT_HTTPHEADER option because some servers refuse connectios to prevent automated requests. In Firefox you have live http headers to see what browser does.
Also make sure the Location: header contains the absolute url and not a relative path according to http 1.1.
If that dosen't work you can use the option CURLOPT_HEADER with curl_info to catch the 302 and redirect it manually.
Here i post an example to do it manually so you check if would produce an infinite loop.
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
$target = ADDR;
curl_setopt($ch, CURLOPT_URL, $target);
$page = curl_exec($ch);
$curl_info = curl_getinfo($ch);
if ($curl_info['http_code'] == 302 || $curl_info['http_code'] == 301)
{
$response_headers = substr($page, 0, $curl_info['header_size']);
if (preg_match('#Location: (.*)#', $response_headers, $location_header))
{
// Call again curl to follow location; Better to wrap the curl process in a function called follow_location
// echo $location_header return an Array
// echo $location_header[0] return "Location: http//blablabla"
// echo $location_header[1] return URL only "http://blablbalba.com" and you can process with cURL :D
echo $location_header[1];
}
}
I am using PHP 5.3.6 and it seems I am unable to make a PUT request using CURL for PUTting just a string.
function put_data($url, $data)
{
$useragent="SimpleAgent-1.0";
$fh = fopen('php://memory', 'rw');
fwrite($fh, $data);
rewind($fh);$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
fclose($fh);
return $result;
}
Here, $data is the string that I want to PUT.
Doesn't work and returns the following error:
500 Internal Server Error The server has either erred or is incapable
of performing the requested operation.
expected string or buffer
I used your code so defined a url and filled the data with a string and all worked as expected. Being I was rejected by the website as there was no receiving end that could deal with a put. To get info easily just add the line
curl_setopt($ch, CURLOPT_VERBOSE, true);
and you will get something like:
* About to connect() to yyyy.xxxx.com port 80 (#0)
* Trying 62.221.196.28...
* connected
* Connected to yyyy.xxxx.com (zz.221.196.28) port 80 (#0)
> PUT / HTTP/1.1
User-Agent: SimpleAgent-1.0
Host: yyyy.xxxx.com
Accept: */*
Content-Length: 14
Expect: 100-continue
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 405 Method Not Allowed
< Date: Thu, 09 Feb 2012 19:46:28 GMT
< Server: Apache
< Allow: GET,HEAD,POST,OPTIONS
< Vary: Accept-Encoding
< Content-Length: 231
< Content-Type: text/html; charset=iso-8859-1
<
* Connection #0 to host yyy.xxxx.com left intact
* Closing connection #0
As you can see from the logging the request went out, however when you want to put the data, the apache setting doesn't allow you to put data to that url. So depending upon the server you will have to take care of a receiving url that accepts the PUT.
I am only able to pass the array as a data with the versions I am using. So this is what I am doing now:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_COOKIE, $curl_cookie);
$arr = array();
if (isset($data)) {
$arr['my_data'] = $data;
}
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arr));
curl_exec($ch);
I have been using curl with PHP for a while. Today I've been trying to fetch http://www.webhostingstuff.com/category/Best-Hosting.html and I keep getting http code 0, which is new to me.
I set the headers
$s->headers = array(
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: en-gb,en;q=0.5",
"Accept-Encoding: gzip, deflate",
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"Keep-Alive: 115",
"Connection: keep-alive",
"Referer: https://google.com"
);
and I have a cookie file (which has nothing in it when the script finishes loading)
Here's the curl function
function fetch($url, $username='', $data='', $proxy=''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
if(isset($proxy)) {
curl_setopt($ch,CURLOPT_TIMEOUT,30);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'proxyadmin:parola');
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT,true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
if(!empty($username)) {
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie/{$username}.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie/{$username}.txt");
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if (is_array($data) && count($data)>0) {
curl_setopt($ch, CURLOPT_POST, true);
$params = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
if (is_array($this->headers) && count($this->headers)>0){
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
}
$this->result = curl_exec($ch);
$curl_info = curl_getinfo($ch);
$header_size = $curl_info["header_size"];
$this->headers = substr($this->result, 0, $header_size);
$this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->error = curl_error($ch);
curl_close($ch);
}
I've also tried to execute via SSH from a different server(in case it's IP blocked)
[brian#ip-184-168-22-244 ~]$ curl -url http://www.webhostingstuff.com/addcomments/5ite.html
Enter host password for user 'rl':
curl: (7) couldn't connect to host
[brian#ip-184-168-22-244 ~]$
How might I resolve this?
Your command
curl -url http://www.webhostingstuff.com/addcomments/5ite.html
should have been:
curl --url http://www.webhostingstuff.com/addcomments/5ite.html
cURL thinks you are specifying the -u option, which is used to specify a username, hence the error message you got. You need to specify --url (two dashes).
Hope that at least helps with the debugging.
Statuscode 0 means the connection was closed (gracefully) before any output was returned.
I guess I'd start by figuring out whether you can connect to the machine at all. If you have access to the remote machine it will likely help debugging.
In my case, the http code 0 was being returned because of a connection timeout. By adding
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
I was able to get rid of the error
Yesterday I faced similar problem. I spent 2 hours on this issue. I was on RHEL system. The curl code had below block for authentication:
if($httpCode == 200) {
$_SESSION["username"] = $username;
$_SESSION["password"] = $password;
return array(true, "Login successful. Please wait while you are redirected to home page.");
}else if($httpCode == 401){
return array(false, "Login failure. Incorrect username / password");
}
This code was being used for authentication. In lab environment it returned 200, but on production it returned 0.
Then I made a similar script (which used curl), and run if from command line like php test3.php . This run resulted in 200 status code.
Then out of curiosity I decided to turn off SELinux temporarily by running this command:
setenforce 0
And guess what this worked. You can then properly set context by running setsebool httpd_can_network_connect on
0 code means curl can't find the server you were looking for.
"yahoo.com/whatever" will return 404, while "yahoo.comwhatever" will return 0.
Maybe it was some internal server error? Right now it works:
> GET /category/Best-Hosting.html HTTP/1.1
> User-Agent: HTTP_Request2/0.5.2 (http://pear.php.net/package/http_request2) PHP/5.2.12
> Host: www.webhostingstuff.com
> Accept: */*
> Accept-Encoding: deflate, gzip
>
< HTTP/1.1 200 OK
< date: Sun, 31 Jul 2011 10:57:43 GMT
< server: Apache
< last-modified: Sun, 31 Jul 2011 10:55:00 GMT
< content-encoding: gzip
< vary: Accept-Encoding
< transfer-encoding: chunked
< content-type: text/html
I use HTTP_Request2 pear package as curl wrapper, the code:
$url = 'http://www.webhostingstuff.com/category/Best-Hosting.html';
$request = new HTTP_Request2 (
$url,
HTTP_Request2::METHOD_GET,
array (
'adapter' => new HTTP_Request2_Adapter_Curl(),
'ssl_verify_peer' => false,
)
);
$request->attach(new HTTP_Request2_Observer_Log('log.txt'));
$result = $request->send();
I'm using a PHP script to read an RSS feed in my Flex 4 app. The script works when I put the URL of the feed in the actual script, but I can't get it to work when I try to send the URL as a parameter from a HTTPService in Flex.
Here is the HTTPService from Flex 4 that I'm using:
<mx:HTTPService url="http://talk.6te.net/proxy.php"
id="proxyService" method="POST"
result="rssResult()" fault="rssFault()">
<mx:request>
<url>
http://feeds.feedburner.com/nah_right
</url>
</mx:request>
</mx:HTTPService>
This is the script that works:
<?php
$ch = curl_init();
$timeout = 30;
$userAgent = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_URL, "http://feeds.feedburner.com/nah_right");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo curl_error($ch);
} else {
curl_close($ch);
echo $response;
}
?>
But this is what I actually want to use, but it doesn't work (only line 6 is different):
<?php
$ch = curl_init();
$timeout = 30;
$userAgent = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_URL, $_REQUEST['url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo curl_error($ch);
} else {
curl_close($ch);
echo $response;
}
?>
Here is the request and response output of the HTTPService from the network monitor in Flash Builder 4 (using the PHP script that doesn't work):
Request:
POST /proxy.php HTTP/1.1
Host: talk.6te.net
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Content-type: application/x-www-form-urlencoded
Content-length: 97
url=%0A%09%09%09%09%09http%3A%2F%2Ffeeds%2Efeedburner%2Ecom%2Fnah%5Fright%0A%20%20%20%20%09%09%09
Response:
HTTP/1.1 200 OK
Date: Mon, 10 May 2010 03:23:27 GMT
Server: Apache
X-Powered-By: PHP/5.2.13
Content-Length: 15
Content-Type: text/html
<url> malformed
I've tried putting the URL in " " in the HTTPService, but that didn't do anything. Any help would be greatly appreciated!
The $_REQUEST['url'] value is being urlencoded as opposed to urlencoding just the querystring. Somewhere in your code and/or FLEX service is causing a "double urlencode". Simply url decode it and you should get the value you need. Also, I noticed line feeds and tabs so you might want to trim it as well.
<?php
$ch = curl_init();
$timeout = 30;
$userAgent = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_URL, trim(urldecode($_REQUEST['url'])));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo curl_error($ch);
} else {
curl_close($ch);
echo $response;
}
?>
That's it.