PHP - Curl Request To Valid Url Returning 400 Error.
Hi,
Could somebody please explain to me why the following code returns a 400 error. I've been sitting looking at it for hours and I can't see anything wrong with it, but then again I don't know too much about cURL. Code: [Select] <?php // INSTANTIATE CURL. $curl = curl_init(); // CURL SETTINGS. curl_setopt($curl, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/96966578.xml"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0); // GRAB THE XML FILE. $response = curl_exec($curl); // Get information about the response $responseInfo=curl_getinfo($curl); // Close the CURL connection curl_close($curl); curl_close($curl); // Make sure we received a response from Twitter if(intval($responseInfo['http_code'])==200){ // Display the response from Twitter $content .= "Success code response:". $response; }else{ // Something went wrong $content .= "Error: " . $responseInfo['http_code']; } // SET UP XML OBJECT. $xmlObjTwitter = simplexml_load_string( $response ); $content .="<h3>Your last 10 twitter posts....</h3>\n" ; $content .= "<ul> \n"; $tempCounter = 0; foreach ( $xmlObjTwitter -> item as $item ) { // DISPLAY ONLY 10 ITEMS. if ( $tempCounter < 11 ) { $content .= "<li><a href=\"{$item -> guid}\">{$item -> title}</a></li> "; } $tempCounter += 1; } $content .="</ul>\n"; echo $content ; ?> Similar Tutorialshi everyone, can you please kindly check my code for error Code: [Select] $xmltosend = "<?xml version='1.0'?>\r\n". "<soap:Envelope xmlns:soap='http://www.w3.org/2001/12/soap-envelope' soap:encodingStyle='http://www.w3.org/2001/12/soap-encoding'>\r\n". "<soap:Body xmlns:m='https://www.trustwave.com/'>\r\n". "<request>\n". "<authentication>\n". "<username>username</username>\n". "<password>password</password>\n". "</authentication>\n". "<operation>getResellerProducts</operation>\n". "<params>\n". "<int>12345</int>\n". "</params>\n". "</request>\n". "</soap:Envelope>\n"; $reqheader = "POST /index.php HTTP/1.1\r\n". "Host:www.trustwave.com\r\n". "Content-Type: application/soap+xml; charset=utf-8\r\n". "Content-Length: ".strlen($xmltosend) ."\r\n\r\n"; $url = 'https://testapi.ssl.trustwave.com/3.0/'; // fake - obviosly! $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $xmltosend); curl_setopt($ch, CURLOPT_POST, true); // what to post curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $reqheader); curl_setopt($ch, CURLOPT_USERPWD, "username:password"); $result = curl_exec($ch); curl_close($ch); print $result; im getting this error Code: [Select] Authorization Required This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required. anyone pleeeaassseee...!!! tia I want to be able to send a GET request to another website for example i want to be able to run this http://www.mywebsite.com/blog.php?clear_cache= using curl but when i use curl the website is loaded into my own heres my code Code: [Select] $curl = curl_init(); $json_url = $_SESSION['blog_base'] . 'blog.php?empty_cache='; curl_setopt ($curl, CURLOPT_URL, "$json_url"); $ANSWER=curl_exec ($curl); curl_close ($curl); Hello, I am trying to do multiple curl requests and get the response for each one output to the screen. I have wrote the following code, but only appear to be getting the last response when outputting $result. What am i doing wrong? Is there a way to wait for each curl request to be completed before appending to $result so I can see them all? Thanks very much if (isset($_POST['submit'])) { $result = array(); foreach ($textAr as $line) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate"); $result[] = curl_exec ($ch); } curl_close ($ch); echo "<pre>"; print_r($result); echo "</pre>"; }
I am testing a url for http post request. It connects to the site then it is disconnected. In fire fox it is giving error that the "connection was reset". In google chrome it is giving error that "Unable to load the webpage because the server sent no data". Following is the script. I am new to curl and also new to http request response. The provider of this site tells me that everything is ok and it is sending data but i am not getting any data or any header of this site. <?php $data = '<?xml version="1.0" encoding="UTF-8"?><Request><Source ClientID="test_xml" Password="1234" /><RequestDetails Language="En"><SearchHotelPriceRequest><ServiceDestination DestinationType="city" DestinationCode="477" /><ImmediateConfirmationOnly>1</ImmediateConfirmationOnly><PeriodOfStay><CheckInDate>2012-03-01</CheckInDate><Duration>3</Duration></PeriodOfStay><Rooms> <Room NumberOfRooms="1" NumberOfAdults="2" /> </Rooms></SearchHotelPriceRequest></RequestDetails></Request>'; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, 'https://62.218.28.13:8443/monWebService/Request/v2'); curl_setopt($ch, CURLOPT_POST, $data); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $data = curl_exec($ch); curl_close ($ch); echo $data; ?> When using cURL, how would I be able to include a call inside my get_all that basically will loop through all the next_page, get and store the data and then output it to $response->data when the "next_page" parameter becomes null? **Here is the method**: public function get_all() { return $response->data; } **This is what $response->data is returning as of now** (The cURL code wasn't included here): "paginator": { "total": 3092, "per_page": 500, "current_page": 2, "last_page": 7, "prev_page": "https://oc.com/api/v1/employees/?page=1", "next_page": "https://oc.com/api/v1/employees/?page=3" }, "data": [ { "id": 1592, etc.... Here are all of my unsuccessful attempts: public function get_all() { // $next_url = $response->paginator->next_page; // // foreach ($response as $next => $next_page) { // print_r2($next); // // if ($next_url !== null) { // $next_page = $response->data; // } // } // foreach ($response as $paginator => $next_page) { // if ($next_url !== null) { // $return[] = $response->data; // } // } // var_dump($response->paginator); // if ($next_url !== null) { // $this->get_all($path, $args, $next_url); // } return $response->data; } Edited October 30, 2019 by Sema314 Hey people of php freaks recently, I've been having issues changing a discord webhook [curl] post request to a website [curl] post request. My knowledge in PHP is very limited so, I really would appreciate some help This is the function: function postToDiscord($message) { $data = array("content" => $message, "username" => "Payouts Bot"); $curl = curl_init("https://discordapp.com/api/webhooks/591662537293168650/eyme87xmCuKne4njucoDSqzojq78NaS9x7t4sgP9m-l5EmrvUeZegkM4wok-L-UdBDxo"); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); return curl_exec($curl); } Currently the discord bot transfers the message into a discord channel and, I do not want that. I want it to transfer it onto a website page. As, I said i'm not very keen on this so please help me.
Edited July 3, 2019 by LegitSpiderman Can you see if a request (post/get) is from a curl request or an actual user? I am trying to defeat a little bit of fraud. I am using a sloppy 3rd party API that doesn't return error codes, just error messages. See below Code: [Select] $err = $object->getErrorMessage(); if( strlen($err) > 0 ) { header("Location: step1.php?err=$err"); } As you can see this is sloppy because anyone can just modify the address bar of step1.php and inject their own error message, or javascript or their own HTML buttons, etc. As a work around, I thought maybe I could use POST instead of GET... At least then the "err" variable would be hidden for the most part Any ideas how I can work around this? Thanks! Hi Guys, I have 6 file upload fileds in an array. When i try to return the error messages at this link: http://www.php.net/manual/en/features.file-upload.errors.php i get an error displayed for every upload box. I was wondering if it is possible to simply return 1 error instead of the 6? For example rather than getting "No File Uploaded No File Uploaded No File Uploaded No File Uploaded No File Uploaded No File Uploaded" i simply get "No File Uploaded". Also If only some files are uploaded no error should be displayed. The code i am using is: foreach ($_FILES['image']['error'] as $key => $error) { if ($error == UPLOAD_ERR_NO_FILE) { echo "No File Uploaded"; } I have tried using "break;" which works in leaving only 1 error, however the error is still displayed if less than 6 files are uploaded. All help greatly appreciated. Thanks I have this search script, that finds a username in a table and displays it. <form name="search" method="post" action="<?=$PHP_SELF?>"> Search: <input type="text" name="username" /> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <? //This is only displayed if they have submitted the form $searching = $_POST['searching']; $find = $_POST['username']; if ($searching =="yes") { echo "<h2>Results</h2><p>"; //If they did not enter a search term we give them an error if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } // Otherwise we connect to our Database // We preform a bit of filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //Now we search for our search term, in the field the user specified $data = mysql_query("SELECT username FROM users WHERE upper($find) LIKE'%$find%'"); //And we display the results while($result = mysql_fetch_array( $data )) { echo $result['username']; echo "<br>"; echo "<br>"; } //This counts the number or results - and if there wasn't any it gives them a little message explaining that $anymatches= mysql_num_rows ($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query<br><br>"; } //And we remind them what they searched for echo "<b>Searched For:</b> " .$find; } ?> For some reason it does not work and gives this message: [b]Warning[/b]: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [b]C:\Program Files\EasyPHP5.2.10\www\yanille\users.php[/b] on line [b]153[/b] Error: Unknown column 'MI' in 'where clause' The reason it says MI is because I searched mi in the database which is part of the username in mike123 What would be the problem? I think I did make the query correct. Didn't I? Thanks for the help ahead of time. This is the full error...Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in This is what I am trying to do... 1. get data from the page before which it gets fine in the $_GET function... 2. then set the sersion[username] to $username...(I think I am doing something wrong there) 3. then if the $username doesnt match the username pulled from the database (according to the $_GET info) then open a different databse and update a value in that table. 4. then kill the code die (); someone please help I have been trying to figure this out all day Code: [Select] <?php $username = $_SESSION['username']; $deleted = $_GET['deleted']; // Connect to MySQL $connect = mysql_connect("db","user","pass") or die("Not connected"); mysql_select_db("dv") or die("could not log in"); // grab the information according to the isbn number $query = "SELECT * FROM 'boox' WHERE isbn='$deleted'"; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { if ($username==$row['username']) { echo"Welcome, ".$_SESSION['username'].""; // Delete entry where isbn number matches accordingly mysql_query("DELETE * FROM 'boox' WHERE isbn='$deleted'"); } elseif ($username!=$row['username']) { $username = $_SESSION['username']; $connect = mysql_connect("db","user","pass") or die("Not connected"); mysql_select_db("db") or die("could not log in"); // run a query to deactivete a account $acti = mysql_query("UPDATE 'desiredusers' SET activated='2' WHERE username='$username'"); $byebye = "SELECT * FROM 'desiredusers' WHERE isbn='$deleted'"; $results = mysql_query($byebye); while($row2 = mysql_fetch_array($results)) echo "here".$username."here"; die("Your account is deactivated contact College Boox Store to get your account back."); } else echo "there is nothing being checked" . $username . ""; } ?> Im working on a "Friend-request"-script, but something is wrong in my "answer"-script... See the script below: Code: [Select] if (isset($_POST['submit_ansReq_answer'])) { $answer = $_POST['ansReq_what']; $uid= $_SESSION['userid']; $fid= $_POST['fid']; $db=friend_db; $dbname=bannaky_basic; include('config.php'); ///////////// IF ACCEPTED if ($answer == "acceptReq") { $reqStat = "YES"; $reqEcho = "Answered YES"; } ///////////// ELSE IF DENIED else if ($answer == "denyReq") { $reqStat = "NO"; $reqEcho = "Answered NO"; } $query = "UPDATE $db SET req_accept='$reqStat' WHERE friend_id='$uid' AND user_id='$fid'"; mysql_query($query); echo "$reqEcho"; If the user hits the Accept-button, everything works fine and is registered in the SQL database. But when hitting the Deny-button nothing happens... I get to the page and $regEcho works, but $reqStat=NO; wont save in the SQL database... Probably a simple solution to this that im not seeing.... Please help me out before i loose my mind. Hello all, I'm new to PHP and new to this forum (although I have benefitted from your help already -cheers!). However, this time I cannot find the answer I need/recognise/understand.. I have a form and want to conduct tests on each field returning an error message as a session variable if the test fails. The test will be different for some of the fields, and the error message is specific to each field. If there is an error in any one of the fields I want to be redirected to a failure page where all of the error messages are displayed, otherwise I am sent on to another page. I have already written and tested a function to sanitise the incoming form data, so that's not a problem - it's just how to loop through and test. I can guess that there are many ways to do this but I need to understand why one option is better than another, and follow the syntax used (it's all part of my steep learning curve) The approach I have thought to use is to create an array holding the field name, the test and the message, then loop through using foreach, applying the array values into the test and creating the error message....but it's not working for me. The other method is to declare a variable $Stop='No' and if the loop identifies an error, part of the output is to change this to 'yes' and through that redirect to the error page. I'd really welcome your advice and tuition....cheers.. my code so far is... Code: [Select] $Stop='No'; $StaffPassCheck=sanitisealphanum($_POST['PasswordCheck']); $Errors[0]['value']= sanitisealphanum($_POST['FirstName']); $Errors[0]['message']='Please re-enter your name'; $Errors[0]['test']=($StaffFname=""); $Errors[1]['value']= sanitisealphanum($_POST['Surname']); $Errors[1]['message']='Please re-enter your surname'; $Errors[1]['test']=($StaffSname=""); $Errors[2]['value']= sanitisealphanum($_POST['Post']); $Errors[2]['message']='You must select an option'; $Errors[2]['test']=($StaffPost="Select Value"); $Errors[3]['value']= sanitisealphanum($_POST['Username']); $Errors[3]['message']='You must select an option'; $Errors[3]['test']=($StaffUser=""); $Errors[4]['value']= sanitisealphanum($_POST['Password']); $Errors[4]['message']='Please re-enter your password'; $Errors[4]['test']=($StaffPass=""); $Errors[5]['value']= sanitisealphanum($_POST['PasswordCheck']); $Errors[5]['message']='Sorry, your passwords do not match'; $Errors[5]['test']=($StaffPass===$StaffPassCheck); foreach ($Errors as $key => $Value){ if ( $Errors['test']=true ){ $Stop='Yes'; return $_SESSION[$key]=$Value['message']; } } if ($Stop='Yes'){ header('Location.test.php'); die(); }else{ header('Location.indexp.php'); } Hi everyone... When I run my index.php (that make a query to my xml server) file, I have the following error:"Failure when receiving data from the peer" Anybody can give a help? Regards Hi, I have used cURL to login in to a site and successful passed and stored the cookie, but I can't get it to submit a form on another page. When I echo the result it is just blank - although I can retrieve other pages fine. I think the page is trying to redirect, which is fine I just want to see if it has submitted or not! In case its useful the form is self referencing and was built using MOOJ Proforms (by some one else). Any ideas? Code: [Select] $post_data['m4j-180'] = 'Yes'; $post_data['m4j-181'] = 'No'; $post_data['m4j-182'] = 'Yes'; $post_data['m4j-170'] = 'This is a test'; $post_data['submit'] = 'send'; //turn key pairs into strings and combine them (key1=value1&key2=value2) foreach ($post_data as $key => $value) { $post_items[] = $key . '=' . $value; } $post_string = implode ('&', $post_items); //Open connection -- address is the "action" setting of the form $curl_connection = curl_init('http://www.testsite.com/index.php?option=com_proforms&jid=6&cid=-1&Itemid=54'); //Set connection options //Set connection to timeout after 30 secs so script doesn't hang curl_setopt($curl_connection, CURLOPT_CONNCTTIMEOUT, 180); //Tell destination URL to output browser content not mobile etc curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); //Tells curl to out results as string not as a display curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); //false below means no error will trigger if SSL not signed or out of date curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); //follow location redirects in header of destination file curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, true); //tell curl which string to post curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string); //execute the post $result = curl_exec($curl_connection); //see any errors that might have happened print_r(curl_getinfo($curl_connection)); //see the error number and description echo curl_errno($curl_connection) . '-' . curl_error($curl_connection); //close connection curl_close($curl_connection); echo $result I get curl error 7 about 30% of the time. The text for the error is cURL error number: 7 - Error: Failed to connect to xxx.xxx.xxx.xxx No buffer space available Any thoughts? so im trying to display error message which is (Error!</font> Currently no updates available.) if nothing from the url returns but all i see is the error message when i try to check for error the data which is available does not show. why so? Code: [Select] function weather(){ $ch = curl_init("http://www.domain.com/weather.html"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if(curl_errno($ch)){ $page = curl_exec($ch); preg_match('#<table[^>]*>(.+?)</table>#is', $page, $matches); foreach ($matches as &$match) { $match = $match; } echo '<table height=\"200px\">'; echo $matches[1]; echo '</table>'; }else{ echo "<table>"; echo "<tr>"; echo "<td>"; echo '<font align="center" color="red">Error!</font> Currently no updates available.'; echo "</td>"; echo "</tr>"; echo "</table>"; } } Hi all i am trying to use curl function recursively but when the function first execute it get the content but next time it give the following error Protocol http not supported or disabled in libcurl Code: [Select] $url = "http://www.example.com"; pager($url); function pager($url){ $urlArray = array(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); echo curl_error($ch); // code to get url from result // i get an array here $urlArray curl_close($ch); foreach($urlArray as $value){ $urlval = urlget($value); if($urlval) { $urlval = str_replace("href=","",$urlval); $urlval = str_replace("'","",$urlval); echo $urlval.'<br>'; pager($urlval); } } } Hey guys, I'm having a strange problem with cURL that I'm drawing an absolute blank to. It works completely fine locally, but when I upload the files to my webhost and test it over there, it doesn't work at all - it loads for about 2 minutes, and then displays: Server error. The website encountered an error while retrieving http://www.craigwatcher.me/playground/units.php. It may be down for maintenance or configured incorrectly. I've checked the error logs but nothing new is being added into there. Basically I'm just iterating through a list of URLs and cURLing each of them. Let me show you the code that I'm working with: ini_set('max_execution_time',0); require_once('../system/utilities.php'); // This will give us our getLocations() function, which has been tested to work 100% both locally and online $categories = array('jjj', 'ggg', 'bbb', 'sss', 'hhh'); $locations = getLocations(); if (!$locations) exit; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_TIMEOUT, 10); foreach ($locations as $country => $cities) { $countryCode = getCountryCode($country); foreach ($cities as $city) { foreach ($categories as $category) { $url=$city.'.en.craigslist.'.$countryCode.'/'.$category.'/index.rss'; echo $url.'<br/>'; curl_setopt($ch, CURLOPT_URL, $url); $rss = curl_exec($ch); if (!$rss) echo ' FAILED to load: '.$url.'<br/>'; } } } curl_close($ch); I've already tested everything else (eg. the getLocations() and getCountryCode(), and they prove to work 100% fine both locally and online. What could it POSSIBLY be!? I'm pulling my hair out over here, my mind is boggling and I'm completely lost at what could possibly be going wrong. Hi I am new to php, I am trying to capture the url and place into a variable but I only get the 1st digit to show, I just cant see what I am doing wrong. Sorry to ask such a basic question but I just can't work it out, I have attached a screen shot of all me code, your help would be very very much appreciated. |