WordPress – Curl on page returns 200 but browser returns 404

I had a weird issue on a PHP script. I was returning an error 404 on a WordPress page the following way :

// At the beginning of a template page
global $wp_query;

  if ( $posts_query->post_count == 0 ) {
    $wp_query->set_404();
    status_header(404);
    get_template_part( 404 ); die();
  }

When I was accessing the error page on the browser, It worked well, I had a 404 error page displayed + the correct status code when I inspected the page (Network tab).

However, when I was “curl-ing” the same error page, it didn’t work. It didn’t work in the shell, or in the PHP script.

The Shell command I did :

$ curl -I http://myerrorpage.dev 
HTTP/1.1 200 OK
Date: Tue, 13 Mar 2018 16:24:08 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.22
Expires: Thu, 19 Nov 2018 08:52:00 GMT
Cache-control: max-age=0, no-store
Set-Cookie: wordpress_fd4fa864532af79923254c1877e41f25=+; expires=Mon, 13-Mar-2017 16:24:09 GMT; Max-Age=-31536000;

The buggy PHP Script

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,            $url);
curl_setopt($ch, CURLOPT_HEADER,         true);
curl_setopt($ch, CURLOPT_NOBODY,         true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT,        1);
$headers[] = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($status);

Solution

I had to remove the “CURLOPT_NOBODY” to get the correct status code.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,            $url);
curl_setopt($ch, CURLOPT_HEADER,         true);
// curl_setopt($ch, CURLOPT_NOBODY,         true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT,        1);
$headers[] = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($status);

 


Permission issue with npm while installing react-devtools

I had this error once while trying to install the npm package react-devtools.

leiluspocus$ npm i -g react-devtools
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  { Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!   stack: 'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/leiluspocus/.npm/_logs/2018-02-27T13_52_59_313Z-debug.log

The issue was that I was previously installing npm packages using the root user.

leiluspocus:lib$ ls -l
total 120 
(...)
drwxr-xr-x  11 root            wheel  374 27 fév 14:52 node_modules

It’s a mess afterwards and it’s easier to handle when the owner is your current user. So, I changed the owner of my folder node_modules using the following command :

sudo chown -R $USER /usr/local/lib/node_modules/

And it worked like a charm !

leiluspocus:lib$ ls -l
total 120
(...)
drwxr-xr-x  11 leiluspocus  wheel  374 27 fév 14:52 node_modules 
leiluspocus:lib$ npm i -g react-devtools
/usr/local/bin/react-devtools -> /usr/local/lib/node_modules/react-devtools/bin.js

> electron@1.8.2 postinstall /usr/local/lib/node_modules/react-devtools/node_modules/electron
> node install.js

Downloading SHASUMS256.txt
[============================================>] 100.0% of 3.43 kB (3.43 kB/s)
+ react-devtools@3.1.0
added 239 packages in 37.236s

 


Getting started with React Native

It’s pretty impressive how we can simply set up a development environment without XCode using Expo.

Make sure you’re using the right Node and NPM versions and double check that there are no missing packages while doing the npm install.

I had to run these commands before running npm start on my app (created through create-react-native-app ):

sudo sysctl -w kern.maxfiles=5242880
sudo sysctl -w kern.maxfilesperproc=524288

 

While you edit your project files (using any IDEA like Atom or Sublime Text), Expo provides live reloading so that you can directly test your application very quickly.

I made a very simple app that only embed a webview. IT can be found here: https://github.com/leiluspocus/react-test

 

Resources:

  • Basic tutorial to get started with React Native: https://facebook.github.io/react-native/docs/tutorial.html
  • Packages explorer for React: https://js.coach/react-native/

 






FieldHelper cannot be cast to java.lang.String pentaho

I had the following error in a “User Defined Java Class” step on Pentaho Data Integration.

FieldHelper cannot be cast to java.lang.String

Indeed, you have to use the getRow() method to convert the field you’re getting into a String or any other variable type.

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException, Exception
{
  Object[] r = getRow();
        if (r == null) { return false; }
        String myString = get(Fields.In, "FeedSubmissionId").getString(r);
}

 


Get XML Data won’t parse XML object – Pentaho

I had an issue with Pentaho today. It wasn’t able to simply parse this XML that I was fetching from Amazon Marketplace Webservice.

Here’s the data I got :

<SubmitFeedResponse xmlns="http://mws.amazonaws.com/doc/2009-01-01/">
<SubmitFeedResult>
<FeedSubmissionInfo>
<FeedSubmissionId>MY_FEED_ID</FeedSubmissionId>
<FeedType>_POST_INVENTORY_AVAILABILITY_DATA_</FeedType>
<SubmittedDate>2016-08-01T19:28:30+00:00</SubmittedDate>
<FeedProcessingStatus>_SUBMITTED_</FeedProcessingStatus>
</FeedSubmissionInfo>
</SubmitFeedResult>
<ResponseMetadata>
<RequestId>MY_REQUEST_ID</RequestId>
</ResponseMetadata>
</SubmitFeedResponse>

The step “Get XML Data” couldn’t parse it because of the xmlns attribute tag. So I had to add a step “Replace in string” that would delete this attribute.

Screenshot from 2016-08-01 17:18:56