Unable to access Heroku login using cURL and PHP

A straightforward PHP script I created for logging into a specific webpage runs smoothly on my localhost using Wamp server. However, when I attempt to run it on Heroku, I encounter a response like this: [image link](https://i.stack.imgur.com/uilks.png).

https://i.stack.imgur.com/V82Nt.png

The script itself is quite simple:

<?php

$username = "username";
$password = "password";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "url");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'user[username]'=>$username, 'user[password]'=>$password)));
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$content = curl_exec($ch);

echo $content;

?>

I'm puzzled as to why the script doesn't work on Heroku. Could there be an issue with Heroku or am I simply lucky that it functions correctly on my Wamp server? I've also tested running the script on other free hosting platforms, but they don't support the CURLOPT_FOLLOWLOCATION parameter, causing the script to fail.

Answer №1

The header displayed on this page is the one you requested using:

curl_setopt ($ch, CURLOPT_HEADER, true);

It's worth noting that CURLOPT_POST is automatically included when using CURLOPT_POSTFIELDS, so there's no need to specify it separately.

Your code snippet should resemble something like this:

<?php

$username = "username";
$password = "password";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.yoursite.com");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'user[username]'=>$username, 'user[password]'=>$password)));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$content = curl_exec($ch);

echo $content;

Answer №2

It appears that the webpage I attempted to publish on has restrictions based on location.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring Long Polling Choices: Nginx, PHP, and Node.js

Currently, I am working on developing a real-time app that will deliver rapid updates to potentially a large number of users. This app will be running alongside a website that operates with a standard content management system (CMS). Both the app and the w ...

Obtain the URL path for accessing URL links using PHP

To obtain the URL of an image, I typically use this code on the main websites: <a href="./index.html"><img src="./images/logo.png" class="" alt=""></a> However, for sub-sites, I need to use a slightly different approach: <a href=".. ...

How can one identify a client's Facebook User ID using PHP upon their initial visit?

Imagine a scenario where a user, who may or may not have an account on my website, logs into Facebook in one browser tab. They then proceed to open another tab and visit my site for the first time on that particular computer. Since there are no pre-existin ...

Issue with .get() method in jQuery affecting IE7, IE8, and IE9

While testing the sending and receiving of data to a PHP file using jQuery, I encountered an issue with Internet Explorer (I am using IE9, but I also checked on IE8 and IE7). When I click on one of my divs, I send an "ID" to the server and the PHP file re ...

Utilizing the route::resource with secure HTTP in Laravel 4

When it comes to enforcing HTTPS on a named route in Laravel, the official documentation suggests the following approach: Route::get('foo', array('https', function() { return 'Must be over HTTPS'; })); In my initial Lara ...

How is the maximum number of concurrent users for a server or LAMP stack determined?

Embarking on the exciting journey of building my own server to gain hands-on experience and understanding of the process. The plan is to set up a robust LAMP stack on this server, leveraging its power as the backend for my mobile application catering to t ...

Guide to utilizing CURL and SAML TOKEN authentication for invoking REST APIs

Here is an example of my URL: https://<ip:port>/TestRESTServices/objects/test-folder Below is the JSON data I would like to send: { "name":"test-1", "parent-uuid":"126" } I am trying to create a folder named test-1 by making a POST request ...

Encountered an unexpected "<" error while using Ajax with MySQL

I am trying to accomplish a simple task - retrieve a table from a MySQL database. Previously, I was using basic MySQL code and it was working fine. However, after switching to MySQLi, I encountered an error: Uncaught SyntaxError: Unexpected token < ...

Exporting Doctrine2 entities to an array

For my application, I have a Product entity that has a many-to-one relationship with the Category entity. I want to store instances of the Product entity in session. Initially, I tried implementing the \Serializable interface on the Product entity. My ...

Adjusting the PHP variable value using an onclick event

I am facing a challenge with increasing a variable $count = 10; every time a user clicks on the <a id="load-more" href="#">Load more</a> Despite trying various methods such as onclick, variable equations, and functions, I have been unsuccessfu ...

Validation request on Laravel was unsuccessful due to missing data input

When performing a validation request, no data is passed and the following error message is displayed: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'longitude' cannot be null (SQL: update settings set longitude = , latitude = , zo ...

Learn how to make an Ajax request in Laravel 5 using XAMPP server

routes.php Route::get('/login',function(){ $data = 'Login'; return View::make('user.login'); }); Route::post('/login',function(){ $formData = Input::get('userData'); parse_str($formData, $ ...

Designing Interactive Circular Dates on Website

Currently, I am working on a webpage using HTML, CSS, JavaScript, and PHP. The goal is to design a page that features 7 circles representing the current date and the next 6 days. Users should be able to click an arrow to navigate to the following 7 days. ...

The SQL script is infiltrating the class code

Currently, I'm in the process of working on my DB class. During testing, I encountered a situation where the code was injected. To illustrate this issue, I inserted a small JS script into field1 and observed the following outcome: https://i.stack.img ...

Transforming the content of a <div> into a variable in PHP

There is a website where you can place orders for various products. The titles of the products available to order are embedded in the HTML code like this: <div class="whatever"> Title </div> I am trying to extract this "title" and assi ...

Unable to open fancybox from a skel-layer menu

Seeking assistance with integrating a Fancybox inline content call from a Skel-layer menu (using the theme found at ) <nav id="nav"> <ul> <li><a href="#about1" class="fancybox fancybox.inline button small fit" >about< ...

The error message "Your session has expired" is displayed by the Wysiwyg

The WysiwygPro editor displays a dialog error message stating, "Your editing session has expired. This is usually caused by a long period of inactivity. Please re-load the page," when clicking on any controls such as Image, Media, Emoticon, etc. This iss ...

Loading JSON data into a form using AJAX from a PHP script

I am using AJAX to populate a form with JSON data from a PHP script. When I click a button, the input fields are filled with data, but not all of them. It seems like jQuery is having trouble accessing the entire DOM, even though I have tried both "window.o ...

Leveraging MySQL CASE for efficiently updating thousands of rows

We have a customer usage table with columns id and voice_activity, where the latter expects a date in the format MM-DD-YYYY to indicate when usage was last displayed. Processing large log files (up to 200k lines) is not an issue, but efficiently updating t ...

PHP Script for Retrieving MailChimp Lists

My current project involves creating a .JSON file to extract a list of subscribers from my MailChimp account. Following the guidelines provided on the website, I have drafted the following code snippet: { "name": "export/export", "description": "M ...